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.
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:
Get a list from Pandas DataFrame column headers
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)
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
This will output:
List of column headers
['A', 'B', 'C']