thangvc91
thangvc91

Reputation: 333

Filter in Pandas by logic "and"

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

Upvotes: 1

Views: 435

Answers (2)

Glenn Kees
Glenn Kees

Reputation: 106

or just

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

Upvotes: 1

wwnde
wwnde

Reputation: 26686

Use regex, starts with in the str.contains

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

Upvotes: 3

Related Questions