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.

Syntax

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

</>
Copy
DataFrame.columns = new_column_names

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

Example

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

Example.py

</>
Copy
import pandas as pd

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

Output

    fruit  quantity
0   apple        40
1  banana        50
2  cherry        60

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

Conclusion

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