Display All Column Names in a DataFrame using Python Pandas



To display only the column names in a DataFrame, use dataframe.columns.

Import the required library with an alias −

import pandas as pd;

Following is the DataFrame −

dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80]
   }
)

Example

Display the column names with dataframe.columns. Following is the complete code −

import pandas as pd;

# create a DataFrame
dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80]
   }
)

print"DataFrame ...\n",dataFrame

# get all the column names
print"\n Display all the column names ...\n",dataFrame.columns

Output

This will produce the following output −

DataFrame ...
       Car        Place  Units
0      BMW       Delhi    100
1     Audi   Bangalore    150
2      BMW   Hyderabad     50
3    Lexus  Chandigarh    110
4    Tesla        Pune     90
5    Lexus      Mumbai    120
6  Mustang      Jaipur     80

Display all the column names ...
Index([u'Car', u'Place', u'Units'], dtype='object')
Updated on: 2021-09-16T08:21:07+05:30

923 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements