[英]Pandas dataframe if 3 consecutive numbers
I have a dataframe df
我有一个数据框
df
Price
0 3
1 3
2 3
3 -3
4 3
5 3
I wanted to make a column which says TRUE
if there are 2 positive numbers in a row如果一行中有 2 个正数,我想创建一列表示
TRUE
So the output is所以输出是
Price output
0 3 FALSE
1 3 TRUE
2 3 TRUE
3 -3 FALSE
4 3 FALSE
5 3 TRUE
Use .shift(1)
to look at the value in the previous row:使用
.shift(1)
查看前一行的值:
df['two_positive'] = (df['Price'] >= 0) & (df['Price'].shift(1) >= 0)
Result:结果:
Price two_positive
0 3 False
1 3 True
2 3 True
3 -3 False
4 3 False
5 3 True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.