How to Rename Multiple Columns in pandas

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

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

1. Quick Examples of Pandas Rename Multiple Columns

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


# Below are some quick examples

# 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.


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

2. Rename Multiple Columns by Names

rename() method is used to rename column names, this method takes dictionary type columns param. To rename multiple columns, create a dict with key-value pair and pass this as 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.


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.


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)

Conclusion

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

References

pandas rename multiple columns

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing How to Rename Multiple Columns in pandas