简体   繁体   中英

Apply pandas groupby aggregation on multiindex

I have a pandas DataFrame

                    A

   foo   one        3
         two        2
         three      4
   bar   one        1
         two        5
         three      2

I want to add a column with relative values for each row, based on the sum of all rows from the first index level. The sum for all values in foo would be 3+2+4=9, so the relative value for the first row would be 3/9 = 0.33.

The resulting DataFrame would look like that:

                    A    rel

   foo   one        3     0.33
         two        2     0.22
         three      4     0.44
   bar   one        1     0.125
         two        5     0.625
         three      2     0.25

I already tried to use groupby on the DataFrame , but i can only figure out how to apply a method to one groupby dimension.

You can use groupby with transform sum :

df['rel'] = df.A / df.groupby(level=0)['A'].transform(sum)
print df
           A       rel
foo one    3  0.333333
    two    2  0.222222
    three  4  0.444444
bar one    1  0.125000
    two    5  0.625000
    three  2  0.250000

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM