In pandas, how to calculate "countif" based on a moving window?

Considering

A = pd.DataFrame([[1, 5, -2], [2, 4, -4], [3, 3, -1], [4, 2, 2], [5, 1, 4]],
             columns=['A', 'B', 'C'], index=[1, 2, 3, 4, 5])

Let's say you want to calculate the number of observations <0 in column C from the last 3 observations based on rolling. In excel, you will translate the "countif" calculation to the specified window with the condition, and the desired result will be:

D = # of x < 0 on a rolling window basis of size 3

A
Out[79]: 
   A  B  C  D
1  1  5 -2  
2  2  4 -4
3  3  3 -1  3
4  4  2  2  2
5  5  1  4  1

How can I do this in an efficient way (Pythonic) using Pandas?

thank

+4
source share
1 answer

You can use rolling_sumin the bools column:

>>> A["D"] = pd.rolling_sum((A["C"] < 0), 3)
>>> A
   A  B  C   D
1  1  5 -2 NaN
2  2  4 -4 NaN
3  3  3 -1   3
4  4  2  2   2
5  5  1  4   1

This works because True ~ 1 and False ~ 0, and we have

>>> A["C"] < 0
1     True
2     True
3     True
4    False
5    False
Name: C, dtype: bool
+4
source

All Articles