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?
Use the syntax new_DataFrame = DataFrame[(DataFrame[column]==criteria1) operator (DataFrame[column2]==criteria2)] , where operator is & or | , to filter a pandas. DataFrame by multiple columns.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With