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
source
share