鉴于以下数据框,我试图获取一个排除所有值为 1 的行的数据框
columns a b c d e f g h i
index
foo 1 0 0 1 1 0 0 1 1
bar 1 1 1 1 1 1 1 1 1
bas 0 1 1 1 1 0 1 1 1
qux 0 1 0 1 1 0 0 0 0
因此,从上面我们应该得到一个不包含像这样的条形行的数据框。
columns a b c d e f g h i
index
foo 1 0 0 1 1 0 0 1 1
bas 0 1 1 1 1 0 1 1 1
qux 0 1 0 1 1 0 0 0 0
我想知道是否有类似 df.dropna 的东西,但有一个值。
df.drop('其中行包含全 1', axis=0)
最佳答案
我会使用==
和~all()
:
>>> df
a b c d e f g h i
foo 1 0 0 1 1 0 0 1 1
bar 1 1 1 1 1 1 1 1 1
bas 0 1 1 1 1 0 1 1 1
qux 0 1 0 1 1 0 0 0 0
>>> (df == 1).all(axis=1)
foo False
bar True
bas False
qux False
dtype: bool
>>> df[~(df == 1).all(axis=1)]
a b c d e f g h i
foo 1 0 0 1 1 0 0 1 1
bas 0 1 1 1 1 0 1 1 1
qux 0 1 0 1 1 0 0 0 0
请注意,这里我们也可以简单地编写 df[~df.all(axis=1)]
,因为您只使用 0 和 1。
关于python - Pandas DataFrame 提取所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17662887/