I have a Pandas dataframe resulting from a groupby() operation. This dataframe has two indexes (year, month). How can I normalize a column relative to the corresponding month in a specific year?
My dataframe looks like the following:
value
year month
2000 1 1234
2 4567
2001 1 2345
2 5678
2002 1 3456
2 6789
I would like to the resulting dataframe to have each value divided by the corresponding monthly value in 2002. Thus, expressing all values relative to 2002 levels. This would result in the values for 2002 being 1.0 for both months.
What is the most efficient way of doing this? Appreciate any help!
Use DataFrame.div
with a level
argument.
df.div(df.xs(2002), level=1, axis=0)
value
year month
2000 1 0.357060
2 0.672706
2001 1 0.678530
2 0.836353
2002 1 1.000000
2 1.000000
Where,
df.xs(2002)
value
month
1 3456
2 6789
The division is aligned along the first level of the 0 th axis.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.