rename column in pandas

How to Rename Columns in Pandas

Pandas is a powerful python library that allows you to easily analyze and process data. Dataframe is the most important component of Python pandas. It is like a data table that offers many functions and methods for fast data manipulation. When you load data from a CSV file or other source, it is stored as a dataframe for optimal processing. Sometimes you may need to change the name of columns in Python dataframe. In this article, we will look at the different ways to rename columns in Pandas.


How to Rename Columns in Pandas

Here are the different ways to rename columns in pandas.


1. Rename Specific Columns

You can rename only specific columns, instead of all columns in Pandas. Here is the syntax to rename specific columns.

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

In the above command, df is the pandas dataframe, old_col1, old_col2 are the old column names and new_col1, new_col2 are the new column names.

Here is an example to rename specific columns. In this following example, we have created a dataframe with 4 columns team, points, assists, rebounds. We use df.rename() function to rename team column name to team_name, and points to points_scored. We display the column names before as well as after renaming them.

import pandas as pd

#define DataFrame
df = pd.DataFrame({'team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#list column names
list(df)

['team', 'points', 'assists', 'rebounds']

#rename specific column names
df.rename(columns = {'team':'team_name', 'points':'points_scored'}, inplace = True)

#view updated list of column names
list(df)

['team_name', 'points_scored', 'assists', 'rebounds']


2. Rename All Columns

If you want to rename all columns of a dataframe, you can use df.columns() function to assign new column names.

df.columns = ['new_col1', 'new_col2', 'new_col3', 'new_col4']

In the above command, new_col1, new_col2, new_col3, new_col4 are the new column names of dataframe.

Here is a simple example to rename all column names of dataframe. Here we have renamed the four columns of dataframe from team, points, assists, rebounds to _team, _points, _assists, _rebounds. We have also displayed the column names before and after the renaming.

import pandas as pd

#define DataFrame
df = pd.DataFrame({'team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#list column names
list(df)

['team', 'points', 'assists', 'rebounds']

#rename all column names
df.columns = ['_team', '_points', '_assists', '_rebounds']

#view updated list of column names
list(df)

['_team', '_points', '_assists', '_rebounds']

Please note, since we only specify the new column names using a list of column names, python will use the 1st element of list to rename 1st column, second element of list to rename second column, and so on.


3. Replace Specific Characters in Column names

Sometimes you may only want to replace, or remove specific characters in column names. You can do so using df.column.str.replace() function.

df.columns = df.columns.str.replace('old_char', 'new_char')

In the above command, we specify the old and new characters. Here is an example where we use replace() function to remove special character $ from our column names. We display new as well as old column names.

import pandas as pd

#define DataFrame
df = pd.DataFrame({'$team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   '$points': [25, 12, 15, 14, 19, 23, 25, 29],
                   '$assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   '$rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#list column names
list(df)

['team', 'points', 'assists', 'rebounds']

#rename $ with blank in every column name
df.columns = df.columns.str.replace('$', '')

#view updated list of column names
list(df)

['team', 'points', 'assists', 'rebounds']

In this article, we have learnt several ways to rename columns in python pandas. They are very useful in case the column names of your input data source such as CSV, or text files, are not as per your requirement and you want to change them after loading them into a Python Dataframe.

Also read:

How to Create Pandas Dataframe from Dictionary
How to Create Pandas Dataframe from Lists
How to Access Index of Last Element in Pandas Dataframe
MySQL INSERT or UPDATE if Exists
How to Do Regular Expression Replace in MySQL

Leave a Reply

Your email address will not be published.