![](/img/trans.png)
[英]How to remove consecutive pairs of opposite numbers from Pandas Dataframe?
[英]Pandas dataframe if 3 consecutive numbers
我有一個數據框df
Price
0 3
1 3
2 3
3 -3
4 3
5 3
如果一行中有 2 個正數,我想創建一列表示TRUE
所以輸出是
Price output
0 3 FALSE
1 3 TRUE
2 3 TRUE
3 -3 FALSE
4 3 FALSE
5 3 TRUE
使用.shift(1)
查看前一行的值:
df['two_positive'] = (df['Price'] >= 0) & (df['Price'].shift(1) >= 0)
結果:
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.