简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM