How to rename columns in Pandas DataFrame?

Pandas DataFrame | Renaming Columns: In this tutorial, we will learn how can we rename one or all columns of a DataFrame in Python? By Pranit Sharma Last updated : April 10, 2023

Columns are the different fields that contain their particular values when we create a DataFrame. We can perform certain operations on both rows & column values. Sometimes we might need to rename a particular column name or all the column names. Pandas allows us to achieve this task using pandas.DataFrame.rename() method.

Rename Pandas DataFrame's Columns

To rename a particular column name or all the column names, use DataFrame.rename() method. This method is used to rename the given column name with the new column name or we can say it is used to alter axes labels.

Syntax

# Standard Syntax
DataFrame.rename(
    mapper=None, *, 
    index=None, 
    columns=None, 
    axis=None, 
    copy=True, 
    inplace=False, 
    level=None, 
    errors='ignore'
    )

# Short Syntax or 
# Syntax to rename a particular column
DataFrame.rename(
    columns = {'old_col_name':'new_col_name'}, 
    inplace = True
    )

While using DataFrame.rename(), we need to pass a parameter as a dictionary of old column names and new column names in the form of keys and values. One more parameter (inplace = True) is need to be passed to change the name of a particular column.

Note: If we want to change all the column names in one go, we will use DataFrame.columns directly to assign a list of new column names by using following syntax:

DataFrame.columns = ['new_col_name1', 'new_col_name2', 'new_col_name3', 'new_col_name4']

Let us now understand how to rename a particular column name and all the column names with two different examples.

Python program to rename particular columns in Pandas DataFrame

# Importing pandas package
import pandas as pd

# Creating a dictionary of student marks
d = {
    "Peter":[65,70,70,75],
    "Harry":[45,56,66,66],
    "Tom":[67,87,65,53],
    "John":[56,78,65,64]
}

# Now, Create DataFrame and 
# assign index name as subject names
df = pd.DataFrame(d, index=["Maths","Physics","Chemistry","English"])

# Printing the DataFrame
print("DataFrame before renamaing column names...")
print(df)

# Renamaing column name "Harry" to "Mike"
# and, "Tom" to "Jason"
df.rename(columns={'Harry':'Mike','Tom':'Jason'},inplace=True)

# Printing the DataFrame
print("DataFrame after renamaing column names...")
print(df)

Output

DataFrame before renamaing column names...
           Peter  Harry  Tom  John
Maths         65     45   67    56
Physics       70     56   87    78
Chemistry     70     66   65    65
English       75     66   53    64
DataFrame after renamaing column names...
           Peter  Mike  Jason  John
Maths         65    45     67    56
Physics       70    56     87    78
Chemistry     70    66     65    65
English       75    66     53    64

Python program to rename all columns in Pandas DataFrame

# Importing pandas package
import pandas as pd

# Creating a dictionary of student marks
d={
    "Peter":[65,70,70,75],
    "Harry":[45,56,66,66],
    "Tom":[67,87,65,53],
    "John":[56,78,65,64]
}

# Now, Create DataFrame and assign index name as subject names
df = pd.DataFrame(d,index=["Maths","Physics","Chemistry","English"])

# Printing the DataFrame
print("DataFrame before renamaing columns...")
print(df)

# Renamaing  all the columns
df.columns=['Aman',"Raj","Shobha","Mohit"]

# Printing the DataFrame
print("DataFrame before renamaing columns...")
print(df)

Output

DataFrame before renamaing columns...
           Peter  Harry  Tom  John
Maths         65     45   67    56
Physics       70     56   87    78
Chemistry     70     66   65    65
English       75     66   53    64
DataFrame before renamaing columns...
           Aman  Raj  Shobha  Mohit
Maths        65   45      67     56
Physics      70   56      87     78
Chemistry    70   66      65     65
English      75   66      53     64

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.