python - How to create new column in a df based on multiple conditions? -


i have df 3 columns: v1, v2, v3;where

v1=[a,b,c,a]  v2=[d,d,f,n]  v3=[a,k,i,j]  

what create new columns based on conditions in column v1~v3.

i can single condition,

df['v1_a']=np.where(df['v1']=='a',1,0) 

it gives new column named 'v1_a' 1/0

however, if want create new column based on multiple conditions, not work:

df['v2_flag']=np.where(df['v2']=='f' or df['v2']=='h',1,0) 

how can accomplish this?

in python and , or can give single result , can't overridden have other purposes modules giant row row comparison you're trying do.

you need use symbolic & (and) , | (or), used bit-wise comparisons. these have been re-purposed pandas row row comparison, makes sense being analogous bit-wise comparisons. more of happy coincidence though, these used because these can overridden modules.

because of priority of these , equalities, you'll need parentheses around each term or else calculate | before == isn't want. can use this:

df['v2_flag']=np.where((df['v2']=='f')|(df['v2']=='h'),1,0) 

Comments