user2822693
user2822693

Reputation: 1330

Multiple conditions from single column in dataframe, pandas

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?

Upvotes: 2

Views: 6944

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

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

Upvotes: 5

Related Questions