[英]Find first true value in a row of Pandas dataframe
我有兩個布爾值的數據幀。
第一個看起來像這樣:
b1=pd.DataFrame([[ True, False, False, False, False],
[False, False, True, False, False],
[False, True, False, False, False],
[False, False, False, False, False]])
b1
Out[88]:
0 1 2 3 4
0 True False False False False
1 False False True False False
2 False True False False False
3 False False False False False
如果我只對每行是否有任何True值感興趣,我可以使用any
方法:
b1.any(1)
Out[89]:
0 True
1 True
2 True
3 False
dtype: bool
但是,我希望基於第二個數據框添加一個約束,如下所示:
b2 = pd.DataFrame([[ True, False, True, False, False],
[False, False, True, True, True],
[ True, True, False, False, False],
[ True, True, True, False, False]])
b2
Out[91]:
0 1 2 3 4
0 True False True False False
1 False False True True True
2 True True False False False
3 True True True False False
我想識別第一個數據幀中具有True值的行,如果它是第二個數據幀的第一行中的第一個True值。
例如,這將排除第2行,因為雖然它在第一個數據幀中具有True值,但它是第二個數據幀中的第二個真值。 相反,第1行和第2行在數據幀1中具有真值,這也是數據幀2中的第一個真值。輸出應如下所示:
0 True
1 True
2 False
3 False
dtype: bool
一種方法是使用cumsum
來幫助找到第一個:
In [123]: (b1 & b2 & (b2.cumsum(axis=1) == 1)).any(axis=1)
Out[123]:
0 True
1 True
2 False
3 False
dtype: bool
這是因為b2.cumsum(axis=1)
給出了看到的Trues的累積數量,並且該數字為1且b2本身為True的情況必須是第一個。
In [124]: b2.cumsum(axis=1)
Out[124]:
0 1 2 3 4
0 1 1 2 2 2
1 0 0 1 2 3
2 1 2 2 2 2
3 1 2 3 3 3
作為@ DSM巧妙回答的變體,這種方法對我來說似乎更直觀一些。 第一部分應該是非常明顯的,第二部分找到第一個列號(w / axis = 1
),對於每個數據幀都是真的並進行比較。
(b1.any(axis = 1) & (b1.idxmax(axis = 1) == b2.idxmax(axis = 1))
制定了一個與pshep123解決方案類似的解決方案。
# the part on the right of & is to check if the first True position in b1 matches the first True position in b2.
b1.any(1) & (b1.values.argmax(axis=1) == b2.values.argmax(axis=1))
Out[823]:
0 True
1 True
2 False
3 False
dtype: bool
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.