Get a list from Pandas DataFrame column headers
You can use the DataFrame.columns
attribute to access the column labels of a DataFrame as an Index object. To convert this to a list, you can use the tolist()
method:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
column_list = df.columns.tolist()
print(column_list)
Watch a video course
Python - The Practical Guide
This will output:
['A', 'B', 'C']