0

I am trying to add a calculated running total column to a Pandas dataframe based on another column. In the example below the calculated column (Running Total) should keep a running total for the pay of the employee, and then restart with a new running total when it finishes looping through Employee A.

Input:

Employee    |    Pay   
-----------------------  
A      |       1        
A      |       2         
A      |       3       
B      |       2   
B      |       3    

Output:

Employee    |    Pay   |  Running Total
----------------------------------------  
A      |       1       |      1  
A      |       2       |      3  
A      |       3       |      6  
B      |       2       |      2  
B      |       3       |      5  
harvpan
  • 8,571
  • 2
  • 18
  • 36
ABear
  • 1

1 Answers1

0

You need:

df['Running Total'] = df.groupby('Employee').cumsum()

Output:

  Employee  Pay  Running Total
0  A          1              1
1  A          2              3
2  A          3              6
3  B          2              2
4  B          3              5
harvpan
  • 8,571
  • 2
  • 18
  • 36