python · March 26, 2023 0

How to get column names in Pandas dataframe

Table of Content

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

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.

%d bloggers like this: