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
DataFrame.columns
ADVERTISEMENT
Example
In the following program, we take a DataFrame and read the column names of this DataFrame.
Example.py
import pandas as pd df = pd.DataFrame( {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}) print(df.columns)Try Online
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
import pandas as pd df = pd.DataFrame( {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}) print(list(df.columns))Try Online
Output
['name', 'quant']
Conclusion
In this Pandas Tutorial, we learned how to get column names of DataFrame using DataFrame().columns attribute.