Home > Software engineering >  Filter in Pandas by logic "and"
Filter in Pandas by logic "and"

Time:12-31

I have a dataframe as below:

enter image description here

I would like to take a result with filter city is San Francisco and score > 90, i wrote the code like below:

df[df['city'].str.contains('San') and df['score'] > 90]
df

however, it show the error is "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." . Can someone assist me on this ? Thank you

however, it v

CodePudding user response:

Use regex, starts with in the str.contains

df[(df['city'].str.contains('^[San]')) & (df['score'] > 90)]

CodePudding user response:

or just

df[df['city'].str.contains('San') & (df['score'] > 90)]
  • Related