How to Rename Columns With List in pandas

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 29, 2023
Spread the love

I will explain how to rename columns with a list of values in pandas DataFrame but remember with a list, you should rename all columns. Even if any column you don’t want to rename, still you need to pass the actual column names with the list.

1. Quick Examples of Rename Columns With List

Below are some quick examples of how to rename columns with the list.


# Below are some quick examples

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 all columns with list
cols = ['Courses','Courses_Fee','Courses_Duration']
df.columns = cols
print(df.columns)

# Rename columns with list
cols = ['Courses','Fee','Duration']
df.set_axis(cols, axis=1,inplace=True)
print(df.columns)

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


# Create DataFrame
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 Columns With List

If you have all columns you wanted to rename with a list in pandas DataFrame, you can just assign the list to DataFrame.columns to rename all columns.


# Rename all columns with list
cols = ['Courses','Courses_Fee','Courses_Duration']
df.columns = cols
print(df.columns)

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


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

3. Rename Columns with List using set_axis()

Alternatively, you can use DataFrame.set_axis() method to rename columns with list. use inplace=True param to rename columns on the existing DataFrame object. In case if you do not want to change the existing DataFrame do not use this param, where it returns a new DataFrame after rename.


# Rename columns with list
cols = ['Courses','Fee','Duration']
df.set_axis(cols, axis=1,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 Columns With List


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 all columns with list
cols = ['Courses','Courses_Fee','Courses_Duration']
df.columns = cols
print(df.columns)

# Rename columns with list
cols = ['Courses','Fee','Duration']
df.set_axis(cols, axis=1,inplace=True)
print(df.columns)

Conclusion

In this article, you have learned how to rename columns with list using two different ways. When using set_axis() method, use inplace=True param to rename columns on existing DataFrame. In case if you do not want to change the existing DataFrame do not use this param, where it returns a new DataFrame after rename.

References

Leave a Reply

You are currently viewing How to Rename Columns With List in pandas