Get Column Names of DataFrame
To get column names of DataFrame in Pandas, use pandas.DataFrame.columns attribute. This attribute gives access to the column names of this DataFrame.
In this tutorial, we will learn how to get column names of DataFrame in Pandas using DataFrame.columns attribute.
Syntax
The syntax to access value/item at given row and column in DataFrame is
</>
Copy
DataFrame.columns
Example
In the following program, we take a DataFrame and read the column names of this DataFrame.
Example.py
</>
Copy
import pandas as pd
df = pd.DataFrame(
{'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]})
print(df.columns)
Output
Index(['name', 'quant'], dtype='object')
DataFrame.column returns an object of type Index. We may convert it to list using list() constructor.
Example.py
</>
Copy
import pandas as pd
df = pd.DataFrame(
{'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]})
print(list(df.columns))
Output
['name', 'quant']
Conclusion
In this Pandas Tutorial, we learned how to get column names of DataFrame using DataFrame().columns attribute.