Skip to Main Content

Python for Basic Data Analysis: PD.8 Renaming data

Get started on your learning journey towards data science using Python. Equip yourself with practical skills in Python programming for the purpose of basic data manipulation and analysis.

Renaming data

There are scenarios in which data is presented to us with names or headers which do not suit or accurately describe the data, as such we may use pandas functions to change the name to a more appropriate one.

This function is rename(), which lets you change index names and/or column names. Here we can change the net_sales column in our dataset to profit, doing:

df.rename(columns={'profit': 'net_sales'})
 

rename() allows you to change index or column headers by issuing a index or column parameter.

 

df=df.rename(index={0:'wave 1', 1: 'wave 2'})

 

 

Activity: Renaming data

Try these out!

1. Rename  cost_of_sales to cost

2. Rename the first index to start and the last index to end

Answers for Activity: Renaming Data

import pandas as pd
df=pd.read_csv("Retail dataset.csv")

df.rename(columns={'cost_of_sales':'cost'})

df=df.rename(index={0:'start',len(df)-1:'end'})

print(df)

Video Guides

Further Readings