Pandas Rename Columns
The data you load into a Pandas DataFrame may not always have the column names that you want or maybe you want to rename the columns for better readability.
In such cases, knowing how to rename columns in Pandas can be very useful.
Here we will discuss different ways and multiple different cases to rename columns in Pandas such that it covers your every use case.
- Using the rename() Method
- Using df.columns
- Using set_axis() Method
- String Methods for Column Names
- Renaming Columns with List Comprehension
- Adding Prefix or Suffix to Column Names
- Conclusion
Table of Contents
1. Using the rename() Method
The DataFrame.rename() method is an inbuilt method in Pandas that is used to rename columns and rows of a DataFrame.
To rename a column pass a dictionary to the columns parameter, where each key is the old column name and the corresponding value is the new column name.
df.rename(columns={'old_column_name':'new_column_name'}, inplace=True)
Here, df is the DataFrame and inplace=True will make the changes in the original DataFrame.
Here is a working example:
import pandas as pd
# Creating a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# π Rename single column
df.rename(columns={'A': 'New_A'}, inplace=True)
print("\nUpdated DataFrame:")
print(df)
Output:
Original DataFrame: A B C 0 1 4 7 1 2 5 8 2 3 6 9 Updated DataFrame: New_A B C 0 1 4 7 1 2 5 8 2 3 6 9
Multiple Columns Renaming
Similarlly, to rename multiple columns, pass a dictionary and add all columns you want to rename as key and the new column name as the value.
import pandas as pd
# Creating a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# π Rename multiple columns
df.rename(columns={'A': 'New_A',
'B': 'New_B',
'C': 'New_C'}, inplace=True)
print("\nUpdated DataFrame:")
print(df)
Output:
Original DataFrame: A B C 0 1 4 7 1 2 5 8 2 3 6 9 Updated DataFrame: New_A New_B New_C 0 1 4 7 1 2 5 8 2 3 6 9
2. Using df.columns
Another way to rename columns in Pandas is to use the df.columns attribute. It return a list in which each element is a column name.
So, to rename columns assign a list of new column names to the df.columns attribute.
import pandas as pd
# Creating a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Original DataFrame:")
# π Rename columns using df.columns
df.columns = ['New_A', 'New_B', 'New_C']
print(df)
Output:
Original DataFrame: A B C 0 1 4 7 1 2 5 8 2 3 6 9 New_A New_B New_C 0 1 4 7 1 2 5 8 2 3 6 9
Renaming Specific Columns using df.columns
To rename specific columns with df.columns, you can apply replace() string method to the list of column names.
import pandas as pd
# Creating a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# π Rename specific columns using df.columns
df.columns = df.columns.str.replace('A', 'New_A')
print("\nUpdated DataFrame:")
print(df)
Output:
Original DataFrame: A B C 0 1 4 7 1 2 5 8 2 3 6 9 Updated DataFrame: New_A B C 0 1 4 7 1 2 5 8 2 3 6 9
3. Using set_axis() Method
The set_axis() method can be used to rename both rows and column in Pandas. To rename columns, pass a list of new column names as the first argument and axis=1 (or axis='columns') as the second argument.
You can also use inplace=True to make the changes in the original DataFrame.
import pandas as pd
# Creating a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# π Rename columns using set_axis()
df.set_axis(['New_A', 'New_B', 'New_C'], axis=1, inplace=True)
print("\nUpdated DataFrame:")
print(df)
Output:
Original DataFrame: A B C 0 1 4 7 1 2 5 8 2 3 6 9 Updated DataFrame: New_A New_B New_C 0 1 4 7 1 2 5 8 2 3 6 9
4. String Methods for Column Names
There are many string methods in Python that can be used to rename columns in Pandas. Some of which you can use are:
- str.upper() - Converts all characters to uppercase
- str.title() - Converts the first character of each word to uppercase and the rest to lowercase
Let's look at an example using str.upper() method.
import pandas as pd
# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['NY', 'LA', 'SF']}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# π Rename columns using str.upper()
df.columns = df.columns.str.upper()
print("\nUpdated DataFrame:")
print(df)
Output:
Original DataFrame: Name Age City 0 Alice 25 NY 1 Bob 30 LA 2 Charlie 35 SF Updated DataFrame: NAME AGE CITY 0 Alice 25 NY 1 Bob 30 LA 2 Charlie 35 SF
5. Renaming Columns with List Comprehension
List comprehension can be used to give a programmatic way to rename columns in Pandas.
Let's rename column names and add a prefixes to each column name.
import pandas as pd
# Creating a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# π Rename columns using list comprehension
df.columns = [f'New_{col}' for col in df.columns]
print("\nUpdated DataFrame:")
print(df)
Output:
Original DataFrame: A B C 0 1 4 7 1 2 5 8 2 3 6 9 Updated DataFrame: New_A New_B New_C 0 1 4 7 1 2 5 8 2 3 6 9
6. Adding Prefix or Suffix to Column Names
add_prefix() and add_suffix() methods can be used to add a prefix or suffix to column names in Pandas.
Following example adds a prefix and suffix to column names.
import pandas as pd
# Creating a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# π Add prefix to column names
df = df.add_prefix('New_')
print("\nAdding prefix to column names:")
print(df)
# π Add suffix to column names
df = df.add_suffix('_suffix')
print("\nAdding suffix to column names:")
print(df)
Output:
Original DataFrame: A B C 0 1 4 7 1 2 5 8 2 3 6 9 Adding prefix to column names: New_A New_B New_C 0 1 4 7 1 2 5 8 2 3 6 9 Adding suffix to column names: New_A_suffix New_B_suffix New_C_suffix 0 1 4 7 1 2 5 8 2 3 6 9
Conclusion
In this tutorial, we've covered diverse methods to rename columns in a Pandas DataFrame. Each approach offers unique advantages, from the simplicity of direct assignment to the flexibility of using the rename() method with dictionaries.
Choose the method that best fits your specific use case and data manipulation needs.
Thank you for reading.