Reputation: 7404
I can find where my DataFrame has ALEX
or KIM
in the name column by using
df.query('Name=="ALEX" or Name=="KIM"')
Name Gender
4556 ALEX ('male', 'female')
4560 KIM ('female', 'male')
However, when I use df[df.Name==('ALEX' or 'KIM')]
, the following is returned:
Name Gender
4556 ALEX ('male', 'female')
and when I use df[df.Name==('ALEX' and 'KIM')]
the following is returned:
Name Gender
4560 KIM ('female', 'male')
So, i) How can I find where Name
is ALEX or KIM in a purely pythonic way, and ii) why does the logic behave the way it does for the last two examples?
Upvotes: 1
Views: 105
Reputation:
The element-wise or
operator in pandas is |
. or
operator does not work because it makes a comparison against the whole series df.Name
(thus returns a single value). You can use the element-wise operator like this:
df[(df.Name == "ALEX") | (df.Name == "KIM")]
Or, you can use the isin
method:
df[df.Name.isin(["ALEX", "KIM"])]
Upvotes: 4