PythonPandas:Getrowswherepreviousvaluesaregreaterthan0

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

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

and

 9     4   -78.0

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

回答

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

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.

  • Thanks a lot for your answer!

以上是PythonPandas:Getrowswherepreviousvaluesaregreaterthan0的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>