Python 删除的列包含特定值(不是字符串)

Python 删除的列包含特定值(不是字符串),python,pandas,Python,Pandas,我只是试图选择我的dat框架中只包含大于零的数字的列(我正在删除它们)。但我有一些困难。以下是我正在做的: 顺便说一下,我正在使用sklearn的波士顿数据集 def positivecol(data): for col in data.columns: if data[col].eq(0): data.drop(col,1, inplace=True) return data 有人能给我一个很好的解释吗,请您需要使用方法any def pos

我只是试图选择我的dat框架中只包含大于零的数字的列(我正在删除它们)。但我有一些困难。以下是我正在做的: 顺便说一下,我正在使用sklearn的波士顿数据集

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

有人能给我一个很好的解释吗,请

您需要使用方法
any

def positivecol(data):
    for col in data.columns:
        if (data[col]<0).any():
            data.drop(col,1, inplace=True)
    return data
def positivecol(数据):
对于data.columns中的列:

如果(数据[col]编辑:这是

我们可以使用惯用的解决方案,而不是循环:

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

请提供您的输入和输出示例,以便我们更好地了解您的意图。请参阅此问题的首要答案:这是否回答了您的问题?@G.Anderson谢谢。我使用的是来自sklearn@Joe,我用过它。但是它只给我一个充满布尔值的数据集