0

I am just trying to select the columns of my dat frame that only contains number greater than zero(I am dropping them off). But I am having some hard time.Here is what I am doing doing: I am using the boston dataset from sklearn by the way.

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

Could someone give me a good explanation as well, please

Shyryu
  • 139
  • 1
  • 1
  • 9
  • Please provide a sample of your input and output so we can better understand what you're trying to do – G. Anderson Dec 18 '19 at 16:16
  • See the top answer to this question: https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values – Joe Dec 18 '19 at 16:17
  • Does this answer your question? [How to select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – Dr. H. Lecter Dec 18 '19 at 16:22
  • @G.Anderson thanks. I am using the boston data from sklearn – Shyryu Dec 18 '19 at 16:22
  • @Joe, I used it. However it only gives me dataset full of booleans – Shyryu Dec 18 '19 at 16:24
  • Explicit loops should be a **last resort** when working with Pandas. Can you share an example of your data? – AMC Dec 18 '19 at 16:56
  • @Shyryu Those are probably meant to be used for boolean indexing. Have you read the Pandas docs? – AMC Dec 18 '19 at 17:06
  • This is a duplicate: https://stackoverflow.com/q/31614804/11301900. – AMC Dec 18 '19 at 17:13

2 Answers2

2

EDIT: This is a duplicate of How to delete a column in pandas dataframe based on a condition?

Instead of a loop, we can use an idiomatic Pandas solution:

df.loc[:, df.ge(0).all()]
AMC
  • 2,642
  • 7
  • 13
  • 35
0

You need to use the method any

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