Get list of column headers from a Pandas DataFrame

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.