Sunday, 1 October 2023

How to Get the Column Names of a Pandas DataFrame

‘columns’ attribute of DataFrame return the column names.

Example

dataframe_columns = df.columns
for column in dataframe_columns:
    print(column)

 

Find the below working application.

 

dataframe_column_names.py
import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data)

dataframe_columns = df.columns
for column in dataframe_columns:
    print(column)

 

Output

Name
Age
City

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment