0

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.