Set Column Names for DataFrame

To set column names of DataFrame in Pandas, use pandas.DataFrame.columns attribute. Assign required column names as a list to this attribute.

In this tutorial, we will learn how to set column names of DataFrame in Pandas using DataFrame.columns attribute.


The syntax to access value/item at given row and column in DataFrame is

DataFrame.columns = new_column_names

where new_column_names is a list of new column names for this DataFrame.


In the following program, we take a DataFrame with some initial column names, and update the column names using DataFrame.columns.

import pandas as pd

df = pd.DataFrame(
    {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]})
df.columns = ['fruit', 'quantity']
Try Online


fruit  quantity
0   apple        40
1  banana        50
2  cherry        60

The column names have been set to [‘fruit’, ‘quantity’].


In this Pandas Tutorial, we learned how to set column names of DataFrame using DataFrame().columns attribute.