How to Get Column Names of Pandas DataFrame?

Get DataFrame Column Names

You can access the column names of DataFrame using columns property.

DataFrame.columns

It returns an object. You can access the column names using index.

Example 1: Print DataFrame Column Names

In this example, we get the dataframe column names and print them.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[['Amol', 72, 67, 91],
	['Lini', 78, 69, 87],
	['Kiku', 74, 56, 88],
	['Ajit', 54, 76, 78]],
	columns=['name', 'physics', 'chemistry', 'algebra'])	

#get the dataframe columns
cols = df.columns 

#print the columns
print(cols)
Run this program ONLINE

Output

Index(['name', 'physics', 'chemistry', 'algebra'], dtype='object')

Example 2: Access Individual Column Names using Index

You can access individual column names using the index.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[['Amol', 72, 67, 91],
	['Lini', 78, 69, 87],
	['Kiku', 74, 56, 88],
	['Ajit', 54, 76, 78]],
	columns=['name', 'physics', 'chemistry', 'algebra'])	

#get the dataframe columns
cols = df.columns 

#print the columns
for i in range(len(cols)):
	print(cols[i])
Run this program ONLINE

Output

name
physics
chemistry
algebra

Example 3: Print Columns using For Loop

You can use for loop to iterate over the columns of dataframe.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[['Amol', 72, 67, 91],
	['Lini', 78, 69, 87],
	['Kiku', 74, 56, 88],
	['Ajit', 54, 76, 78]],
	columns=['name', 'physics', 'chemistry', 'algebra'])	

#get the dataframe columns
cols = df.columns 

#print the columns
for column in cols:
	print(column)
Run this program ONLINE

Output

name
physics
chemistry
algebra

Summary

In this Pandas Tutorial, we extracted the column names from DataFrame using DataFrame.column property.