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
?
You can use
cumsum
:And for second
df
addmap
bydict
:Or: