I have a multi index dataframe (df):
working_pattern Full-time Part-time
sex Male Female Male Female
salary_band
Up to 15000 630 208 403 348
15001 - 20000 741 279 170 401
20001 - 25000 929 722 320 596
25001 - 30000 818 571 725 587
30001 - 35000 175 273 711 383
35001 - 40000 212 928 855 929
40001 - 45000 586 937 577 141
45001 - 50000 876 382 995 786
I am trying to sum rows 40001 - 45000
and 45001 - 50000
. So the output looks like:
working_pattern Full-time Part-time
sex Male Female Male Female
salary_band
Up to 15000 630 208 403 348
15001 - 20000 741 279 170 401
20001 - 25000 929 722 320 596
25001 - 30000 818 571 725 587
30001 - 35000 175 273 711 383
35001 - 40000 212 928 855 929
40001 + 1462 1319 1572 927
I have tried:
df["40001 +"] = df.loc[['40001 - 45000','45001 - 50000']].sum()
without success
Use DataFrame.loc
for add new row:
df.loc["40001 +"] = df.loc[['40001 - 45000','45001 - 50000']].sum()
print (df)
working_pattern Full-time Part-time
sex Male Female Male Female
salary_band
Up to 15000 630 208 403 348
15001 - 20000 741 279 170 401
20001 - 25000 929 722 320 596
25001 - 30000 818 571 725 587
30001 - 35000 175 273 711 383
35001 - 40000 212 928 855 929
40001 - 45000 586 937 577 141
45001 - 50000 876 382 995 786
40001 + 1462 1319 1572 927
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.