Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cumulative sum of rows in Python Pandas [duplicate]

I'm working on a dataframe which I get a value for each year and state :

  0  State   1965   1966    1967    1968
  1  Alabama 20.2    40     60.3     80
  2  Alaska   10     15      18      20
  3  Arizona  5      5       10      12

I need each value sum the last with the current one :

  0  State   1965   1966    1967    1968
  1  Alabama 20.2   60.2   120.5    200.5
  2  Alaska   10     25      43      63
  3  Arizona  5      10      20      32

I tried df['sum'] = df.sum(axis=1) and .cumsum but I don't know how to apply it to my problem, as I don't need a new column with the total sum.

like image 361
Delacroix Avatar asked Jan 19 '26 21:01

Delacroix


1 Answers

Use DataFrame.cumsum with axis=1 and convert non numeric column State to index:

df = df.set_index('State').cumsum(axis=1)
print (df)
         1965  1966   1967   1968
State                            
Alabama  20.2  60.2  120.5  200.5
Alaska   10.0  25.0   43.0   63.0
Arizona   5.0  10.0   20.0   32.0

Or select all columns without first and assign back:

df.iloc[:, 1:] = df.iloc[:, 1:].cumsum(axis=1)
print (df)
     State  1965  1966   1967   1968
0                                   
1  Alabama  20.2  60.2  120.5  200.5
2   Alaska  10.0  25.0   43.0   63.0
3  Arizona   5.0  10.0   20.0   32.0
like image 99
jezrael Avatar answered Jan 21 '26 11:01

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!