【问题标题】:dropping columns contains a particular value (not string)删除列包含特定值(不是字符串)
【发布时间】:2019-12-19 01:14:07
【问题描述】:

我只是想选择我的 dat 框架中仅包含大于零的数字的列(我正在删除它们)。但是我遇到了一些困难。这就是我正在做的事情: 顺便说一下,我正在使用来自 sklearn 的波士顿数据集

def positivecol(data):
    for col in data.columns:
       if data[col].eq(0):
          data.drop(col,1, inplace=True)
    return data

谁能给我一个很好的解释,拜托

【问题讨论】:

标签: python pandas


【解决方案1】:

编辑:这是How to delete a column in pandas dataframe based on a condition?的副本

我们可以使用惯用的 Pandas 解决方案来代替循环:

df.loc[:, df.ge(0).all()]

【讨论】:

    【解决方案2】:

    你需要使用any的方法

    def positivecol(data):
        for col in data.columns:
            if (data[col]<0).any():
                data.drop(col,1, inplace=True)
        return data
    

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 2012-03-21
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2022-11-18
      • 2020-09-27
      相关资源
      最近更新 更多