以下是此問題的示例資料框:
name gender address phone no.
---------------------------------------
1 1 1 1
1 0 0 0
1 1 1 1
1 1 0 1
這里所需的輸出是2因為包含所有1的行數是2。
誰能幫我解決這個問題?謝謝。
uj5u.com熱心網友回復:
使用1 來標識值,然后在所有值都為時聚合eq(1)每行,并利用/等價:anyTrueTruesumTrueTrue1
df.eq(1).all(axis=1).sum()
輸出:2
中間體:
df.eq(1)
name gender address phone no.
0 True True True True
1 True False False False
2 True True True True
3 True True False True
df.eq(1).all(axis=1)
0 True
1 False
2 True
3 False
dtype: bool
uj5u.com熱心網友回復:
讓我們做
l = sum(df.eq(1).all(axis=1))
print(l)
2
uj5u.com熱心網友回復:
假設上面的資料框是一個二進制表,即所有值都是1或0,那么df.sum(axis=1)等于4應該給你所有值都是的所有行1。
df[df.sum(axis=1) == len(df.columns)]
name gender address phone no.
0 1 1 1 1
2 1 1 1 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/519487.html
標籤:Python熊猫数据框
上一篇:熊貓系列墊功能不適用于熊貓應用
下一篇:Pandas資料框中列的重復計數
