Q:

How to rename columns in Pandas DataFrame?

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to select rows from a DataFrame based on colum... >>
<< Delete a column from a Pandas DataFrame...