How to rename columns in Pandas DataFrame?

Given a DataFrame, we have to rename a particular column and all columns.
Submitted by Pranit Sharma, on April 03, 2022

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.

pandas.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']

To work with Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

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

1) 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

2) 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 »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.