簡體   English   中英

如果 3 個連續數字,則為 Pandas 數據框

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM