Get column names from Pandas DataFrame as a python List

To get column names from a Pandas DataFrame you can use df.columns.values and if you want to convert it to a list you can use df.columns.values.tolist()

import pandas as pd

df = pd.DataFrame({
    'Name': ['John', 'Eric', 'Rick'],
    'Score': [200, 500, 100\]

df_columns = df.columns.values.tolist()
#prints - ['Name', 'Score']
['Name', 'Score']

In the code snippet, we have created a DataFrame df which has two columns Name and Score. If we want to get the list of column names we can use the below syntax.

Was this helpful?