15.1 C
London
Friday, July 5, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Rename Columns with Dictionary

Pandas: How to Rename Columns with Dictionary

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to rename columns with a dictionary in pandas:

#define dictionary
some_dict = {'old_col1': 'new_col1',
             'old_col2': 'new_col2',
             'old_col3': 'new_col3'}
 
#rename columns in DataFrame using dictionary
df.rename(columns=some_dict, inplace=True)

Note: We must specify inplace=True to modify the column names of the original DataFrame.

The following example shows how to use this syntax in practice.

Example: Rename Columns in Pandas with Dictionary

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'rebounds': [10, 14, 14, 13, 13, 12, 10, 7],
                    'points': [30, 22, 19, 14, 14, 11, 20, 28],
                    'assists': [5, 6, 6, 5, 8, 7, 7, 9]})

#view DataFrame
print(df)

   rebounds  points  assists
0        10      30        5
1        14      22        6
2        14      19        6
3        13      14        5
4        13      14        8
5        12      11        7
6        10      20        7
7         7      28        9

We can use the following syntax to rename each of the columns in the DataFrame using a dictionary:

#define dictionary with new column names
some_dict = {'rebounds': 'rebs',
             'points': 'pts',
             'assists': 'ast'}
 
#rename columns in DataFrame using dictionary
df.rename(columns=some_dict, inplace=True)

#view updated DataFrame
print(df)

   rebs  pts  ast
0    10   30    5
1    14   22    6
2    14   19    6
3    13   14    5
4    13   14    8
5    12   11    7
6    10   20    7
7     7   28    9

Notice that each of the columns have been renamed based on the values we specified in the dictionary.

It’s worth noting that you don’t have to rename every single column using a dictionary.

For example, we could create a dictionary to only rename the points and assists columns in the DataFrame:

#define dictionary with new column names for points and assists only
some_dict = {'points': 'pts',
             'assists': 'ast'}
 
#rename columns in DataFrame using dictionary
df.rename(columns=some_dict, inplace=True)

#view updated DataFrame
print(df)

   rebounds  pts  ast
0        10   30    5
1        14   22    6
2        14   19    6
3        13   14    5
4        13   14    8
5        12   11    7
6        10   20    7
7         7   28    9

Only the points and assists columns were renamed.

Since the rebounds column was not included in the dictionary, it was not renamed in the DataFrame.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to List All Column Names in Pandas
How to Sort Columns by Name in Pandas
How to Drop Duplicate Columns in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories