7

I am trying to subset a pandas dataframe based on values of two columns. I tried this code: df[df['gold']>0, df['silver']>0, df['bronze']>0] but this didn't work.

I also tried: df[(df['gold']>0 and df['silver']>0). This didn't work too. I got an error saying:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What would you suggest?

Pranay Aryal
  • 5,208
  • 4
  • 30
  • 41

1 Answers1

15

I will answer my own question, hoping it will help someone. I tried this and it worked. df[(df['gold']>0) & (df['silver']>0)]

Note that I have used & instead of and and I have used brackets to separate the different conditions.

Pranay Aryal
  • 5,208
  • 4
  • 30
  • 41