Home > Software engineering >  Pandas adding previous row and current row into current row
Pandas adding previous row and current row into current row

Time:05-08

I have a Data Frame which is around 100x100 and I want to add previous cell current cell = current cell

An example of the Data Frame:

User     A     B    C   D  
Apple    0     5    6   4 
Orange   23    0    3   5 
Grape    5     10   9   6 
Fruit    67    3    0   9

The desired output:

User     A     B    C   D  
Apple    0     5    11  15 
Orange   23    23   26  31 
Grape    5     15   24  30 
Fruit    67    70   70  79

Using Pandas is this possible to do?

CodePudding user response:

The operation is called cumulative sum:

df.cumsum(axis=1)

(I assume that "User" is the index).

  • Related