Site icon Spark By {Examples}

How to Rename Multiple Columns in pandas

pandas rename multiple columns

When working with pandas DataFrames you are often required to rename multiple columns of pandas DataFrame, you can do this by using the rename() method. This method takes columns param that takes dict of key-value pairs, the key would be your existing column name, and the value would be the new column name.

In this article, I will explain how to rename multiple columns by using pandas.DataFrame.rename() method.

Key Points –

1. Quick Examples of Rename Multiple Columns

Below are some quick examples of how to rename multiple columns in Pandas DataFrame.


# Quick examples of pandas rename multiple columns

# Rename Multiple Columns by Name
# Rename Multiple Columns by Index
df.rename(columns={df.columns[1]: 'Fee', df.columns[2]: 'Duration'},inplace=True)
print(df.columns)

Let’s create a simple DataFrame and execute these examples and validate the results.


import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]
column_names=["Courses","Fee",'Duration']
df=pd.DataFrame(technologies, columns=column_names)
print(df.columns)

Yields below column names.


# Output:
Index(['Courses', 'Fee', 'Duration'], dtype='object')

2. Rename Multiple Columns by Names

To rename multiple columns by their names in pandas, you can use the rename() method along with a dictionary specifying the old column names as keys and the new column names as values.

rename() method is used to rename column names, this method takes dictionary type columns param. To rename multiple columns, create a dict with a key-value pair and pass this as a param to the rename method.


# Rename multiple column names by label
df.rename(columns={'Fee': 'Courses_Fee','Duration':'Courses_Duration'},inplace=True)
print(df.columns)

Yields below output. This changes column from Fee to Courses_Fee and from Duration to Courses_Duration.


# Output:
Index(['Courses', 'Courses_Fee', 'Courses_Duration'], dtype='object')

3. Rename Multiple Columns by Index

Now let’s see how to rename multiple column names by index/position in pandas DataFrame. For this, I will be using the same method explained above. To rename multiple columns, you have to pass multiple dictionary mappings in key-value pair to the columns param. To demonstrate with indexes, I will be using df.columns[index] to get the column names for a given position and use it as a key in dict.


# Rename multiple columns by index
df.rename(columns={df.columns[1]: 'Fee', df.columns[2]: 'Duration'},inplace=True)
print(df.columns)

Yields below output. This changes column from Courses_Fee to Fee and from Courses_Duration to Duration.


# Output:
Index(['Courses', 'Fee', 'Duration'], dtype='object')

4. Complete Example of Rename Multiple Columns


import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]
column_names=["Courses","Fee",'Duration']
df=pd.DataFrame(technologies, columns=column_names)
print(df.columns)


# Rename multiple column names by label
df.rename(columns={'Fee': 'Courses_Fee','Duration':'Courses_Duration'},inplace=True)
print(df.columns)

# Rename multiple columns by Index
df.rename(columns={df.columns[1]: 'Fee', df.columns[2]: 'Duration'},inplace=True)
print(df.columns)

Frequently Asked Questions on Rename Multiple Columns in Pandas

How can I rename multiple columns in pandas?

You can use the rename() method in pandas to rename multiple columns simultaneously. Provide a dictionary where the keys are the current column names and the values are the new column names.

Can I rename columns by their positions instead of names?

In pandas, the rename() method renames columns based on their names, not their positions. This means you cannot directly rename columns by their positions using the rename() method. However, you can rename columns based on their positions indirectly by first retrieving the column names using the columns attribute, modifying the list of column names, and then assigning the modified list back to the columns attribute.

Is it possible to rename columns in place?

It is possible to rename columns in place in pandas by using the inplace=True parameter with the rename() method. When you set inplace=True, the original DataFrame will be modified, and the changes will be applied directly to it, without the need to assign the result back to a variable.

What happens if I provide incorrect column names in the renaming dictionary?

If you provide incorrect column names in the renaming dictionary, pandas will not rename those columns, and it will issue a warning to inform you about the columns that were not renamed.

Can I use regular expressions to rename columns in pandas?

The rename() method in pandas does not support the use of regular expressions for renaming columns. The method expects exact matches for column names in the renaming dictionary. However, you can achieve similar functionality by first obtaining the column names, applying regular expressions to modify the list of column names, and then assigning the modified list back to the DataFrame’s columns attribute.

Conclusion

In this article, you have learned how to rename multiple columns in a single statement by using the rename() method.

References

Exit mobile version