python - Pandas DataFrame 提取所有列

标签 python pandas

鉴于以下数据框,我试图获取一个排除所有值为 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/

相关文章:

python - 如何连接向量以在循环中创建数组?

python - 使用 spotipy 提取艺术家流派和歌曲发布日期

python - 在列的子集和索引上合并两个 pandas DataFrame

python - 如何根据其他列的值估算 NaN 值?

python - 使用 python pandas 读取已拆分为多行的行

python - 将 pandas 索引转换为 numpy 数组。 Python

python - 如何使用discord.py 查找用户上次事件的时间?

python - Scikit Logistic 回归汇总输出?

Python 线程 : Running 2 different functions simultaneously

c# - 如何使用 C# 执行协程?