Chrome Extension

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']
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
['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?
Join Devsheet Ask a Question