Pandas DataFrame rename() Method – Explained with Examples

The rename() method in Pandas is a useful tool for renaming the labels of a DataFrame’s index (rows) or columns. This method provides the flexibility to change the names of the columns and rows without directly modifying the original DataFrame. In this blog, we will explore the rename() method in detail with several practical examples.

Basic Usage of rename()

The rename() method is used to rename the labels of a DataFrame. It has the following syntax:

Python
DataFrame.rename(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore')
Parameters
  • mapper: Dictionary or function for renaming.
  • index: Dictionary or function for renaming row labels.
  • columns: Dictionary or function for renaming column labels.
  • axis: {0 or ‘index’, 1 or ‘columns’}, default 0.
  • copy: Also copy underlying data.
  • inplace: Modify the DataFrame in place (do not create a new object).
  • level: In case of a MultiIndex, only rename labels in the specified level.
  • errors: Ignore or raise errors. If ‘raise’, the method will raise an error when a label is not found. If ‘ignore’, the method will skip the missing labels.

Renaming Columns

Example 1: Renaming Columns Using a Dictionary

Let’s start by creating a simple DataFrame:

Python
import pandas as pd

data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

The DataFrame will look like,

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

To rename the columns, we can use a dictionary where keys are the current column names and values are the new column names:

Python
df_renamed = df.rename(columns={'A': 'Alpha', 'B': 'Bravo', 'C': 'Charlie'})
print("\nDataFrame with Renamed Columns:")
print(df_renamed)

Output:

DataFrame with Renamed Columns:

   Alpha  Bravo  Charlie
0      1      4        7
1      2      5        8
2      3      6        9

This example demonstrates renaming DataFrame columns by providing a dictionary with the current and new column names. It shows how to map old names to new ones.

Example 2: Renaming Columns Using a Function

We can also rename columns using a function. For example, let’s convert all column names to uppercase:

Python
df_renamed_to_uppercase = df_renamed.rename(columns=str.upper)
print("\nDataFrame with Uppercase Column Names:")
print(df_renamed_to_uppercase)

Output:

DataFrame with Uppercase Column Names:
   ALPHA  BRAVO  CHARLIE
0      1      4        7
1      2      5        8
2      3      6        9

This example illustrates renaming columns by applying a function, converting all column names to uppercase using str.upper.

Renaming Rows (Index)

Example 3: Renaming Rows Using a Dictionary

Let’s rename the index labels of the DataFrame:

Python
df_renamed = df.rename(index={0: 'Row1', 1: 'Row2', 2: 'Row3'})
print("\nDataFrame with Renamed Rows:")
print(df_renamed)

Output:

DataFrame with Renamed Rows:
      A  B  C
Row1  1  4  7
Row2  2  5  8
Row3  3  6  9

Here, we rename the DataFrame’s index labels using a dictionary, changing numerical indices to more descriptive names like ‘Row1’, ‘Row2’, and ‘Row3’.

Example 4: Renaming Rows Using a Function

Similarly, we can rename the index using a function. For example, let’s prefix the index with ‘Index_’:

Python
df_renamed = df.rename(index=lambda x: 'Index_' + str(x))
print("\nDataFrame with Prefixed Index:")
print(df_renamed)

Output:

DataFrame with Prefixed Index:
         A  B  C
Index_0  1  4  7
Index_1  2  5  8
Index_2  3  6  9

This example, we rename index labels by applying a function, adding a prefix ‘Index_’ to each index label.

Inplace Modification

Example 5: Renaming Columns Inplace

If you want to modify the original DataFrame without creating a new one, set inplace=True:

Python
df.rename(columns={'A': 'Alpha', 'B': 'Bravo', 'C': 'Charlie'}, inplace=True)
print("\nOriginal DataFrame after Inplace Rename:")
print(df)

Output:

Original DataFrame after Inplace Rename:
   Alpha  Bravo  Charlie
0      1      4        7
1      2      5        8
2      3      6        9

In this example, we rename columns directly within the original DataFrame by setting inplace=True, which modifies the DataFrame without creating a copy.

Handling Missing Labels

Example 6: Handling Missing Labels with errors Parameter

If you try to rename a label that does not exist, Pandas will raise an error unless you set errors='ignore':

Python
data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

df_renamed = df.rename(columns={'A': 'Alpha', 'D': 'Delta'}, errors='ignore')
print("\nDataFrame with Ignored Missing Labels:")
print(df_renamed)

Output:

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

DataFrame with Ignored Missing Labels:
   Alpha  B  C
0      1  4  7
1      2  5  8
2      3  6  9

This example demonstrates handling non-existent labels during renaming by setting errors='ignore', allowing the method to skip missing labels without raising an error.

Renaming MultiIndex Levels

Example 7: Renaming MultiIndex Levels

For DataFrames with a MultiIndex, you can rename specific levels using rename():

Python
arrays = [['A', 'A', 'B', 'B'], ['one', 'two', 'one', 'two']]
index = pd.MultiIndex.from_arrays(arrays, names=('Upper', 'Lower'))
df_multi = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], index=index)
print("\nOriginal MultiIndex DataFrame:")
print(df_multi)

df_renamed = df_multi.rename(index={'A': 'Alpha'}, level='Upper')
print("\nMultiIndex DataFrame with Renamed Level:")
print(df_renamed)

Output:

Original MultiIndex DataFrame:
              0   1   2   3
Upper Lower                
A     one     1   2   3   4
      two     5   6   7   8
B     one     9  10  11  12
      two    13  14  15  16

MultiIndex DataFrame with Renamed Level:
              0   1   2   3
Upper Lower                
Alpha one     1   2   3   4
      two     5   6   7   8
B     one     9  10  11  12
      two    13  14  15  16

This example highlights renaming specific levels in a MultiIndex DataFrame, changing the ‘Upper’ level labels from ‘A’ to ‘Alpha’, demonstrating how to target and rename levels in a MultiIndex.

Conclusion

The rename() method in Pandas offers a straightforward and flexible approach to rename columns, rows, or MultiIndex levels . You can make your DataFrames more readable and aligned with your specific naming conventions, improving the overall clarity and maintainability of your code. Understanding its parameters and how to use them effectively can significantly enhance your data manipulation tasks in Pandas.

Also Explore:

Leave a Comment