How to Rename Columns In A Pandas DataFrame?

10 minutes read

In Pandas, renaming columns in a DataFrame can be done using the rename() function. This function allows you to change the names of one or more columns in a DataFrame. Here's how to do it:

  1. First, import the required libraries: pandas.
1
import pandas as pd


  1. Create a DataFrame:
1
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})


The DataFrame will look like this:

1
2
3
   A  B
0  1  3
1  2  4


  1. Use the rename() function to rename the columns:
1
df = df.rename(columns={'A': 'Column1', 'B': 'Column2'})


This will rename the columns 'A' and 'B' to 'Column1' and 'Column2', respectively.

  1. View the updated DataFrame:
1
print(df)


The DataFrame will now look like this:

1
2
3
   Column1  Column2
0        1        3
1        2        4


You can also choose to assign the updated DataFrame to a new variable if you want to keep the original DataFrame unchanged:

1
new_df = df.rename(columns={'A': 'Column1', 'B': 'Column2'})


Remember to specify the mapping of old column names to new column names within the columns parameter of rename(). The mapping can be passed as a dictionary, where keys represent the old column names and values represent the new column names.

Best Python Books of December 2024

1
Learning Python, 5th Edition

Rating is 5 out of 5

Learning Python, 5th Edition

2
Head First Python: A Brain-Friendly Guide

Rating is 4.9 out of 5

Head First Python: A Brain-Friendly Guide

3
Python for Beginners: 2 Books in 1: Python Programming for Beginners, Python Workbook

Rating is 4.8 out of 5

Python for Beginners: 2 Books in 1: Python Programming for Beginners, Python Workbook

4
Python All-in-One For Dummies (For Dummies (Computer/Tech))

Rating is 4.7 out of 5

Python All-in-One For Dummies (For Dummies (Computer/Tech))

5
Python for Everybody: Exploring Data in Python 3

Rating is 4.6 out of 5

Python for Everybody: Exploring Data in Python 3

6
Learn Python Programming: The no-nonsense, beginner's guide to programming, data science, and web development with Python 3.7, 2nd Edition

Rating is 4.5 out of 5

Learn Python Programming: The no-nonsense, beginner's guide to programming, data science, and web development with Python 3.7, 2nd Edition

7
Python Machine Learning: Machine Learning and Deep Learning with Python, scikit-learn, and TensorFlow 2, 3rd Edition

Rating is 4.4 out of 5

Python Machine Learning: Machine Learning and Deep Learning with Python, scikit-learn, and TensorFlow 2, 3rd Edition


What is the difference between inplace=True and inplace=False when renaming columns in a Pandas DataFrame?

When renaming columns in a Pandas DataFrame, inplace=True and inplace=False specify whether to modify the DataFrame in place or return a new DataFrame with the changes.

  • inplace=True: Modifies the DataFrame in place, i.e., the changes are made directly on the original DataFrame.
  • inplace=False: Returns a new DataFrame with the changes, leaving the original DataFrame unchanged. By default, inplace=False.


Example with inplace=True:

1
2
3
4
5
6
7
8
9
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Rename columns using inplace=True
df.rename(columns={'A': 'NewA', 'B': 'NewB'}, inplace=True)

print(df)


Output:

1
2
3
4
   NewA  NewB
0     1     4
1     2     5
2     3     6


The original DataFrame df is modified with new column names.


Example with inplace=False (default behavior):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Rename columns using inplace=False (default behavior)
new_df = df.rename(columns={'A': 'NewA', 'B': 'NewB'})

print(df)     # Original DataFrame is unchanged
print(new_df) # New DataFrame is printed


Output:

1
2
3
4
5
6
7
8
9
   A  B
0  1  4
1  2  5
2  3  6

   NewA  NewB
0     1     4
1     2     5
2     3     6


The original DataFrame df remains unchanged, and a new DataFrame new_df is created with the renames columns.


How to rename columns in a multi-level Pandas DataFrame?

To rename columns in a multi-level pandas DataFrame, you can use the rename() method with a dictionary to map the old column names to the new ones.


Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import pandas as pd

# Create a multi-level DataFrame
data = {
    ('Group 1', 'A'): [1, 2, 3],
    ('Group 1', 'B'): [4, 5, 6],
    ('Group 2', 'A'): [7, 8, 9],
    ('Group 2', 'B'): [10, 11, 12]
}
df = pd.DataFrame(data)

# Rename the columns
df.columns = ['Group 1_A', 'Group 1_B', 'Group 2_A', 'Group 2_B']

# Alternatively, use the rename() method
new_columns = {
    ('Group 1', 'A'): 'Group 1_A',
    ('Group 1', 'B'): 'Group 1_B',
    ('Group 2', 'A'): 'Group 2_A',
    ('Group 2', 'B'): 'Group 2_B'
}
df = df.rename(columns=new_columns)

print(df)


Output:

1
2
3
4
   Group 1_A  Group 1_B  Group 2_A  Group 2_B
0          1          4          7         10
1          2          5          8         11
2          3          6          9         12


In the example above, the rename() method is used to map the old column names to the new names using a dictionary. The DataFrame columns are then updated using the mapped values. Alternatively, you can directly assign the new column names to the columns attribute of the DataFrame.


How to rename columns in a Pandas DataFrame with a suffix or prefix?

You can rename columns in a Pandas DataFrame with a suffix or prefix using the add_suffix() and add_prefix() methods. Here's how you can do it:

  1. Using add_suffix():
1
df = df.add_suffix('_suffix')


This method appends the specified suffix to each column name in the DataFrame.

  1. Using add_prefix():
1
df = df.add_prefix('prefix_')


This method prepends the specified prefix to each column name in the DataFrame.


Here's an example to illustrate how these methods work:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import pandas as pd

# Create a sample DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)

# Rename columns with a suffix
df_suffix = df.add_suffix('_suffix')

# Rename columns with a prefix
df_prefix = df.add_prefix('prefix_')

# Print the original and renamed DataFrames
print(df)
print(df_suffix)
print(df_prefix)


Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
   A  B
0  1  4
1  2  5
2  3  6

   A_suffix  B_suffix
0         1         4
1         2         5
2         3         6

   prefix_A  prefix_B
0         1         4
1         2         5
2         3         6



How to rename columns in a Pandas DataFrame using a combination of old and new names?

To rename columns in a Pandas DataFrame using a combination of old and new names, you can use the rename() method with a dictionary where the keys are the old column names and the values are the new column names.


Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Import the required libraries
import pandas as pd

# Create a sample DataFrame
data = {'OldName1': [1, 2, 3],
        'OldName2': [4, 5, 6],
        'OldName3': [7, 8, 9]}
df = pd.DataFrame(data)

# Create a dictionary to map the old column names to the new column names
column_mapping = {'OldName1': 'NewName1',
                  'OldName2': 'NewName2',
                  'OldName3': 'NewName3'}

# Rename the columns using the dictionary
df.rename(columns=column_mapping, inplace=True)

# Print the DataFrame to see the renamed columns
print(df)


Output:

1
2
3
4
   NewName1  NewName2  NewName3
0         1         4         7
1         2         5         8
2         3         6         9


In this example, the columns are renamed using a combination of old and new names by creating a dictionary (column_mapping) that maps the old names to the new names. The rename() method is then used to rename the columns using the dictionary. The inplace=True argument is used to modify the DataFrame in place, i.e., without creating a new copy. Finally, the DataFrame is printed to see the renamed columns.


How to rename columns in a Pandas DataFrame based on a condition?

To rename columns in a Pandas DataFrame based on a condition, you can use the rename() function along with a lambda function or a dictionary comprehension to apply the renaming logic.


Here's an example that demonstrates how to rename columns based on a condition:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

# Rename columns based on condition
new_columns = df.columns.map(lambda x: x + '_new' if 'A' in x else x)
df.rename(columns=dict(zip(df.columns, new_columns)), inplace=True)

# Print the renamed DataFrame
print(df)


Output:

1
2
3
4
   A_new  B  C
0      1  4  7
1      2  5  8
2      3  6  9


In the above example, we used columns.map() to apply the renaming logic, where we appended '_new' to the column name if 'A' is present in the original name, otherwise, we kept the original name. Finally, we used rename() to assign the new column names to the DataFrame. Note that by setting inplace=True, the original DataFrame is modified.


You can modify the condition and renaming logic as per your requirements.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To rename a column in a pandas DataFrame, you can use the rename() method and specify the old column name as the key and the new column name as the value in a dictionary. For example, if you have a DataFrame called df and you want to rename the column "old...
To rename a column in pandas when the column name contains a space, you can use the rename function and specify the old column name with the space enclosed in quotes. For example, if you have a DataFrame df with a column named "First Name", you can ren...
To bulk rename files in PowerShell, you can use the Rename-Item cmdlet.First, navigate to the directory containing the files you want to rename using the Set-Location cmdlet.You can use various parameters with the Rename-Item cmdlet to rename files based on sp...