【问题标题】:Pandas DataFrame extract columns where allPandas DataFrame 提取所有列
【发布时间】:2013-07-16 04:07:32
【问题描述】:

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

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    我会使用==~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。

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 1970-01-01
      • 2022-11-06
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2019-02-03
      • 2020-01-28
      • 1970-01-01
      相关资源
      最近更新 更多