Appearance
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:
Get a list from Pandas DataFrame column headers
python
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">Watch a video course Python - The Practical Guide
</div>
This will output:
List of column headers
console
['A', 'B', 'C']