Saturday, November 16, 2024
Google search engine
HomeLanguagesGet list of column headers from a Pandas DataFrame

Get list of column headers from a Pandas DataFrame

In this article, we will see, how to get all the column headers of a Pandas DataFrame as a list in Python. 

The DataFrame.column.values attribute will return an array of column headers.

Get list of column headers from a Pandas DataFrame

pandas DataFrame column names

Using list() Get Column Names as List in Pandas DataFrame

In this method we are using Python built-in list() function the list(df.columns.values), function. 

Python3




# import pandas library
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]})
 
print("The DataFrame :")
display(df.head())
 
# multiple ways of getting column names as list
print("\nThe column headers :")
print("Column headers from list(df.columns.values):",
      list(df.columns.values))
print("Column headers from list(df):", list(df))
print("Column headers from list(df.columns):",
      list(df.columns))


Output : 

get list of columns in pandas dataframe using list()

Using list() to get columns list from pandas DataFrame

Note: Here we have display() function, which works inside Jupyter notebook for presentation purpose. For running in any other IDE, you can replace display() function with print() function.

Using tolist() Get Column Names as List in Pandas DataFrame

In this method, we are importing Python pandas module and creating a DataFrame to get the names of the columns in a list we are using the tolist(), function.

Python3




# import pandas library
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 using tolist()
print("The column headers :")
 
print(df.columns.tolist())
# or we could also use
# print(df.columns.values.tolist())
# or,
# print(df.columns.to_numpy().tolist())


Output : 

The DataFrame :
  Students   BMI   Religion
0        A  22.7      Hindu
1        B  18.0      Islam
2        C  21.4  Christian
3        D  24.1       Sikh
The column headers :
['Students', 'BMI', 'Religion']

Using list comprehension Get Column Names as List in Pandas DataFrame

In this method we are importing a Pandas module and creating a Dataframe to get the names of the columns in a list we are using the list comprehension

Python3




# 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 using list comprehension
print([col for col in df])


Output:

Get list of column headers from a Pandas DataFrame

List of column names using List comprehension

RELATED ARTICLES

Most Popular

Recent Comments