How to use tolist() Get Column Names as List in Pandas DataFrame In Python Pandas

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']

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.

pandas DataFrame column names

Similar Reads

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....

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

...

Using list comprehension 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....

Contact Us