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'})
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)
You are expected to comply with University policies and guidelines namely, Appropriate Use of Information Resources Policy, IT Usage Policy and Social Media Policy. Users will be personally liable for any infringement of Copyright and Licensing laws. Unless otherwise stated, all guide content is licensed by CC BY-NC 4.0.