2

I am trying to select a subset from a single dataframe column, and I need help applying two conditions on a single column. For example, how do I select for both "Tom" and "Chris" in the table below?

import pandas as pd
dic={"Name":["Chris","Tom","Steven"], "Age":[12,34,55]}
dic={"Name":["Chris","Tom","Steven"], "Age":[12,34,55]}
df=pd.DataFrame(dic)
df[df["Name"]=="Tom"]

Why is it that when I use df[df["Name"]==("Chris" or "Tom")] it picks "Chris, but when or is replaced by and, "Tom" is selected?

1
  • You are misunderstanding the order of operations. The first thing that happens is "Chris" or "Tom" which is a pretty bizarre operation. Python has 'truthiness' for strings. Any non-empty string evaluates as True. Follow MaxU's explanation for the rest. Commented Sep 12, 2017 at 16:01

1 Answer 1

5

when we check condition1 OR condition2 - it's enough if first condition/operand is True, so if the first one is True - the second will not be checked (because it's enough to have one True):

In [247]: 1 or 2
Out[247]: 1

for AND we must check also the second one if the first one is True (because all conditions must be True):

In [248]: 1 and 2
Out[248]: 2

but if the first condition is False we don't need to check the second one (because it's enough to have one False - it'll make the whole "thing" False):

In [250]: 0 and 1
Out[250]: 0

The same logic will be applied to strings (NOTE: empty strings will be evaluated as False):

In [242]: ("Chris" or "Tom")
Out[242]: 'Chris'

In [249]: ("Chris" and "Tom")
Out[249]: 'Tom'

so when you do

df[df["Name"]==("Chris" or "Tom")]

it's the same as:

df[df["Name"]=="Chris"]

how to do it properly (in a Pandas way) :

In [243]: df[df["Name"].isin(["Chris","Tom"])]
Out[243]:
   Age   Name
0   12  Chris
1   34    Tom
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.