I have a Pandas Dataframe with one column called [A] and different values:
[A]
1
1
4
5
6
7
5
4
1
1
1
I would like to create another column -called B- and sum the values of A when A is > 2. If this condition is not true the result should be 0.
Finally the result of my example should be:
[B]
0
0
4
9
15
22
27
31
0
0
0
Doing this in Excel is pretty straightforward because you can sum the value of [A] to the previous [B] value. But I don’t know how to do it with Python. I have tried:
DF['B'] = np.where(DF['A'] > 2, DF['A'] + DF['B'].shift(-1), 0)
But it does not work.
Any help would be greatly appreciated.
Thanks!