askvity

How to Rename Columns in a DataFrame?

Published in DataFrame column rename 3 mins read

You can efficiently rename columns in a DataFrame using the rename() method.

Renaming one or more columns in a DataFrame is a common operation in data manipulation. The most straightforward and recommended way to achieve this is by utilizing the DataFrame's built-in rename() method.

Using the rename() Method

As highlighted in the provided reference, we can use the rename() method to rename one or more columns of a DataFrame. This method is highly flexible and allows you to specify exactly which columns you want to change.

The rename() method takes a dictionary as its primary argument for column renaming. In this dictionary:

  • The keys represent the current or old names of the columns you wish to rename.
  • The values represent the new names you want to assign to those columns.

Here's a breakdown of how it works:

  1. Create the mapping dictionary: Define a dictionary where keys are the existing column names and values are the desired new names.
  2. Call the rename() method: Pass the dictionary to the columns parameter of the rename() method.

Practical Example

Let's illustrate with a simple example, similar to the one described in the reference where columns 'A' and 'B' are renamed.

Suppose you have a DataFrame like this:

A B C
1 5 9
2 6 10
3 7 11
4 8 12

You want to rename column 'A' to 'X' and column 'B' to 'Y'.

import pandas as pd

# Create a sample DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8],
        'C': [9, 10, 11, 12]}
df = pd.DataFrame(data)

print("Original DataFrame:")
print(df)

# Define the renaming dictionary
rename_map = {'A': 'X', 'B': 'Y'}

# Rename the columns using the rename() method
df_renamed = df.rename(columns=rename_map)

print("\nDataFrame after renaming:")
print(df_renamed)

As demonstrated, applying df.rename(columns={'A': 'X', 'B': 'Y'}) results in a new DataFrame where the columns 'A' and 'B' have been renamed to 'X' and 'Y', respectively, just as described in the reference.

X Y C
1 5 9
2 6 10
3 7 11
4 8 12

The rename() method returns a new DataFrame by default, leaving the original DataFrame unchanged. If you wish to modify the DataFrame in place without creating a new one, you can use the inplace=True argument:

# Rename columns in place
df.rename(columns={'A': 'X', 'B': 'Y'}, inplace=True)

Using rename() with a dictionary provides a clear, concise, and efficient way to manage column names, especially when renaming multiple columns simultaneously.

Related Articles