简体   繁体   中英

Pandas DataFrame iterating over rows and sum

I have got this pandas DataFrame:

 recipe_name   ingredient_group       weight%
 pudding       milk                     0.60
 pudding 2     sugar                    0.10
 pudding 2     sugar                    0.70
 pudding 2     milk                     0.30
 pudding 3     egg                      0.20

I would like a pandas DataFrame like this:

 recipe_name   ingredient_group       weight%     new_column
 pudding       milk                     0.60      0.60
 pudding 2     sugar                    0.10      0.80 (0.1+0.7)
 pudding 2     sugar                    0.70      0.80 (0.1+0.7)
 pudding 2     milk                     0.30      0.30
 pudding 3     egg                      0.20      0.20

The problem is that there are recipes that use the same ingredient multiple times (for example pudding 2 uses 2 times sugar).

I would like to create an extra column with the weight% and then with the sum of an ingredient that was used twice (see example above).

I tried to make a for loop for this problem, but I didn't succeed. Anyone an idea?

Use groupby with transform sum :

df['accumulated weight'] = df.groupby(['name','group'])['weight'].transform(sum)
print (df)
    name     group  weight  accumulated weight
0  Appie  elephant      60                  60
1  Henry     tiger      50                 120
2  Henry     tiger      70                 120
3  Laura       cow      30                  30
4  Laura     tiger      20                  20

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