Python Pandas: Get rows where previous values are greater than 0

Mic*_*506 2 python dataframe pandas

I have this dataframe in pandas:

   col1    col2
0     1    -0.5
1     2    -4.0
2     8     3.0
3     9     6.0
4     4   345.0
5     6    -7.0
6     7  3456.0
7    47     3.0
8     2     5.0
9     4   -78.0
Run Code Online (Sandbox Code Playgroud)

I want to return only these rows, where the "col2" values of the previous 3 rows are greater than 0. In this case:

5     6    -7.0
Run Code Online (Sandbox Code Playgroud)

and

 9     4   -78.0
Run Code Online (Sandbox Code Playgroud)

I have tried it with rolling but cant find a way. Can you help me, please? :)

Tom*_*Tom 5

Very similar to the answer by SeaBean, but without using the apply:

>>> df[df['col2'].shift().gt(0).rolling(3).sum().eq(3)]
   col1  col2
5     6  -7.0
9     4 -78.0
Run Code Online (Sandbox Code Playgroud)

You use shift so you don't have to consider the previous 3 rows, rather each row and the 2 behind it. Then make a mask of positive values (.gt(0)), and with a rolling window of 3 over that mask, check which groups have a sum of 3.

  • 非常感谢您的回答! (2认同)