How to get column names in Pandas dataframe
Published on Aug. 22, 2023, 12:15 p.m.
To get the column names in a Pandas dataframe, you can use the columns
attribute of the dataframe. Here is an example:
import pandas as pd
# create a simple dataframe
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]})
# get the column names as a list
columns = list(df.columns)
# print the column names
print(columns)
This will output:
['a', 'b', 'c']
Alternatively, you can also access the column names using df.keys()
or list(df)
. All of these methods will return a list of the column names in the dataframe.