Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subset pandas dataframe using values from two columns

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?

like image 829
Pranay Aryal Avatar asked Feb 25 '17 20:02

Pranay Aryal


People also ask

How do you filter a data frame in two columns?

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.


1 Answers

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.

like image 133
Pranay Aryal Avatar answered Sep 21 '22 14:09

Pranay Aryal