-1

I have a df like this one:

    player_profile  category    upgrade_n   level   
0   Weak            common_caddie   1       13.0    
1   Weak            common_caddie   1       25.0    
2   Weak            common_caddie   1       36.0    
3   Weak            common_caddie   1       51.0    
4   Weak            common_caddie   1       59.0    
9   Standard Player common_caddie   1       18.0    
10  Standard Player common_caddie   1       36.0    
11  Standard Player common_caddie   1       48.0    

What I need a is cumulative by player_profile and category, but I need to keep the level column. the final output should look like this:

player_profile   category        cumulative_sum   level
Weak             common_caddie   1                 13
Weak             common_caddie   2                 25
Weak             common_caddie   3                 36
Weak             common_caddie   4                 51
Weak             common_caddie   5                 59
Standard Player  common_caddie   1                 13
Standard Player  common_caddie   2                 36
Standard Player  common_caddie   3                 48

I found this post but I couldn't achieve wht I wanted (the level colum was missing).

Thanks everyone for the help!

ianux22
  • 405
  • 4
  • 16

1 Answers1

0

I don't know what is wrong with the link that you show:

df = pd.DataFrame({'profile':['w','w','w','w','m','m','m'],
                   'level': [1,2,3,4,5,6,7]})

Then simply use groupby and cumsum or cumcount.

df['cumsum'] = df.groupby('profile')[['level']].cumsum()
df['cumcount'] = df.groupby('profile').cumcount()+1
PTQuoc
  • 938
  • 4
  • 13
  • When I run this code, it seems that the resulting df is corrupted (I cannot open it or save it to a csv. Maybe you added an extra [ ] on 'level'. Anyway, it's not working – ianux22 Jun 28 '22 at 14:10