• Post author:
  • Post category:Pandas
  • Post last modified:November 4, 2024
  • Reading time:14 mins read
You are currently viewing How to Rename Columns With List in Pandas

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.

Advertisements

Key Points –

  • You can rename columns by directly assigning a list of new names to the DataFrame’s .columns attribute.
  • The order of names in the list should correspond to the order of the existing columns in the DataFrame.
  • Renaming columns with a list modifies the DataFrame in place if using the .columns attribute or set_axis() with inplace=True.
  • The new column names should be unique to avoid confusion when accessing columns later.
  • The set_axis() method allows you to rename columns by specifying the axis parameter, giving more flexibility in renaming.
  • Directly assigning a list to .columns changes column names in place without creating a new DataFrame.

Quick Examples of Rename Columns With List

If you are in a hurry, below are some quick examples of how to rename columns with the list.


# Quick examples of rename columns with list

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

# Example 2: 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("Create DataFrame:\n",df.columns)

Yields below column names.

pandas rename columns list

Rename Columns With List

If you have all the 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("Renamed Columns:\n", df.columns)

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


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

Rename Columns with List Using set_axis()

Alternatively, you can use DataFrame.set_axis() method to rename columns with the list. use the inplace=True param to rename columns on the existing DataFrame object. In case 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')

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)

Frequently Asked Questions on Rename Columns With List in Pandas

How do I rename columns in a Pandas DataFrame using a list?

To rename columns in a Pandas DataFrame using a list, you can use the rename method and provide a dictionary where keys are the current column names and values are the new column names.

Can I rename only specific columns using a list?

You can certainly rename only specific columns in a Pandas DataFrame using a list. Instead of renaming all columns, you can create a dictionary where the keys are the current column names you want to change, and the values are the corresponding new column names.

Can I rename columns based on a pattern using a list?

While directly using a list to rename columns based on a pattern might not be straightforward, you can achieve this by combining the list with other techniques like regular expressions.

Can I use the set_axis() method to rename columns with a list?

You can use the set_axis() method to rename columns with a list in Pandas. The set_axis() method allows you to set the axis labels along a specified axis, and you can use it to rename columns by providing a list of new column names.

What happens if the new column names list has a different length than the number of columns in the DataFrame?

If the new column names list has a different length than the number of columns in the DataFrame, you will encounter a ValueError. The length of the new column names list must match the number of columns in the DataFrame.

Is it possible to create a new DataFrame with the renamed columns instead of modifying the original DataFrame?

It is possible to create a new DataFrame with the renamed columns instead of modifying the original DataFrame. You can use either the rename method or the assignment approach to create a new DataFrame with the renamed columns.

Conclusion

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

References