Lauren
Lauren

Reputation: 23

Multiple Conditions in 1 Variable

I'm wanting to put multiple conditions into one variable so that I can determine the value I will insert into my column 'EmptyCol'. Please see below. Note: This works with one condition but I believe I'm missing something with multiple conditions

Condition = ((df['status']=='Live') and
(df['name'].str.startswith('A') and
(df['true']==1))

df.loc[Condition, 'EmptyCol'] = 'True'

Upvotes: 0

Views: 105

Answers (1)

Abdulwahabdev
Abdulwahabdev

Reputation: 66

Use "&" instead of "and"

Condition = ((df['status']=='Live') &
(df['name'].str.startswith('A') &
(df['true']==1))

also I recomend to use df.at

I got some truble with df.loc sometime !

Condition = ((df['status']=='Live') &
(df['name'].str.startswith('A') &
(df['true']==1))

def ChangeValueFunc(Record):
    df.at[Record['index'],'EmptyCol'] = 'True'


df_2.loc[Condition ,:].reset_index().apply(lambda x:ChangeValueFunc(x) , axis = 1) 

Upvotes: 1

Related Questions