How to rename columns in Pandas DataFrame

 Pandas is a useful library in data analysis, and Pandas DataFrame is Two-dimensional, size-mutable, potentially heterogeneous tabular data. In this tutorial, let’s see how to rename columns in Pandas DataFrame.

There are 3 approaches to rename columns in Pandas DataFrame. Let us look at each of these with examples.

Method 1: Rename Specific column names in Pandas DataFrame

If you would like to rename specific column names in DataFrame, we can do that using the rename() method.

Syntax:

DataFrame.rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True)

The rename() method is used to rename index, columns, rows. We can pass the columns argument with old and new column names to rename the columns in Pandas DataFrame.

The inplace=true argument ensures to change the original DataFrame. If not passed, it takes the default value as false and returns the new DataFrame.

Example – Renaming specific column names using rename() function
# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': ['10', '8', '3', '5'],
                   'runrate': ['0.5', '1.4', '2', '-0.6'],
                   'wins': ['5', '4', '2', '2']})

# print the column names of DataFrame
print(list(df))

# rename the column names of DataFrame
df.rename(columns={'points': 'total_points',
          'runrate': 'run_rate'}, inplace=True)

# print the new column names of DataFrame
print(list(df))

Output

['team', 'points', 'runrate', 'wins']
['team', 'total_points', 'run_rate', 'wins']

Method 2: Rename all column names in Pandas DataFrame

If you would like to rename all the column names in DataFrame, you could simply assign the new column names as a list to the columns attribute of the DataFrame object, as shown below.

Note: You need to provide all the new column names in the list, and you cannot rename only specific columns. 

If you do not pass all the new column names in the list, Python will raise ValueError: Length mismatch: Expected axis has 4 elements, new values have 3 elements
Example: Renaming all column names in Pandas DataFrame
# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': ['10', '8', '3', '5'],
                   'runrate': ['0.5', '1.4', '2', '-0.6'],
                   'wins': ['5', '4', '2', '2']})

# print the column names of DataFrame
print(list(df))

# rename the column names of DataFrame
df.columns = ['_team', '_points', '_run_rate', '_wins']


# print the new column names of DataFrame
print(list(df))

Output

['team', 'points', 'runrate', 'wins']
['_team', '_points', '_run_rate', '_wins']

Method 3: Replace specific characters in Columns of Pandas DataFrame

There are times where we import the data from other sources like excel, DB etc., where the column names may consist of certain special characters like "_", "$", etc.

We can replace these characters with a new character or remove them by replacing an empty character, as shown below.

Example: Replace specific characters in Columns of Pandas DataFrame
# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'_team': ['India', 'South Africa', 'New Zealand', 'England'],
                   '_points': ['10', '8', '3', '5'],
                   '_runrate': ['0.5', '1.4', '2', '-0.6'],
                   '_wins': ['5', '4', '2', '2']})

# print the column names of DataFrame
print(list(df))

# replace "_" with blank in all column names of DataFrame
df.columns = df.columns.str.replace('_', '')


# print the new column names of DataFrame
print(list(df))

Output

['_team', '_points', '_runrate', '_wins']
['team', 'points', 'runrate', 'wins']
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like