Dynamic cumulative summations in Pandas

145 views Asked by At

In the following DataFrame, the column B computes the sum of column A from index 0 to n.

ix    A      B
---------------
 0     1     1
 1     1     2
 2     1     3
 3     1     4
 4     2     6
 5    -1     5
 6    -3     2

Alternatively, the column B sums 1 for each type == 'I' and -1 for each type == 'O'.

ix   type     B
----------------
 0      I     1
 1      I     2
 2      O     1
 3      I     2
 4      O     1
 5      O     0
 6      I     1

How to perform this type of calculations, where the n-th result of one column depends on the aggregated results of another column, up to n?

1

There are 1 answers

0
jezrael On BEST ANSWER

You can use cumsum:

df['C'] = df.A.cumsum()
print (df)
   ix  A  B  C
0   0  1  1  1
1   1  1  2  2
2   2  1  3  3
3   3  1  4  4
4   4  2  6  6
5   5 -1  5  5
6   6 -3  2  2

And for second df add map by dict:

df['C'] = df.type.map({'I':1, 'O':-1}).cumsum()
print (df)
   ix type  B  C
0   0    I  1  1
1   1    I  2  2
2   2    O  1  1
3   3    I  2  2
4   4    O  1  1
5   5    O  0  0
6   6    I  1  1

Or:

df['C'] = df.type.replace({'I':1, 'O':-1}).cumsum()
print (df)
   ix type  B  C
0   0    I  1  1
1   1    I  2  2
2   2    O  1  1
3   3    I  2  2
4   4    O  1  1
5   5    O  0  0
6   6    I  1  1