Skip to content
Related Articles

Related Articles

Get list of column headers from a Pandas DataFrame
  • Difficulty Level : Hard
  • Last Updated : 17 Aug, 2020

Let us see how to get all the column headers of a Pandas DataFrame as a list. The df.columns.values attribute will return a list of column headers.

Example 1 :




# importing pandas as pd
import pandas as pd
  
# creating the dataframe
df = pd.DataFrame({'PassengerId': [892, 893, 894, 895
                                   896, 897, 898, 899],
                   'PassengerClass': [1, 1, 2, 1, 3, 3, 2, 2],
                   'PassengerName': ['John', 'Prity', 'Harry'
                                     'Smith', 'James', 'Amora'
                                     'Kiara', 'Joseph'], 
                   'Age': [32, 54, 71, 21, 37, 9, 11, 54]})
  
display("The DataFrame :")
display(df)
  
# print the list of all the column headers
display("The column headers :")
display(list(df.columns.values))

Output :

Example 2 :




# importing pandas as pd
import pandas as pd
  
# creating the dataframe
my_df = {'Students': ['A', 'B', 'C', 'D'], 
         'BMI': [22.7, 18.0, 21.4, 24.1], 
         'Religion': ['Hindu', 'Islam'
                      'Christian', 'Sikh']}
df = pd.DataFrame(my_df)
display("The DataFrame :")
display(df)
  
# print the list of all the column headers
display("The column headers :")
display(list(df.columns.values))

Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :