简体   繁体   中英

Returning a value in a new column based on 2 column conditions

I am writing a function that will return a value in a new column by checking the values of 2 different columns. If the condition of both columns is satisfied, then the function should return a value in a separate column.

Following is some of the code that I have written

def conversion_flag(df):
    if np.where((df[(df['conversion_month'] == '202010') & (df['oct2020'] == 4)])):
        df['conversion_flag_oct'] = '1'
    elif np.where((df[(df['conversion_month'] == '202011') & (df['nov2020'] == 4)])):
        df['fourg_conversion_flag_nov'] = '1'
    elif np.where((df[(df['conversion_month'] == '202012') & (df['dec2020'] == 4)])):
         df['fourg_conversion_flag_dec'] = '1'
    elif np.where((df[(df['conversion_month'] == '202101') & (df['jan2021'] == 4)])):
        df['fourg_conversion_flag_jan'] = '1'
    else:
        return '0'
    return df

I call the function in this manner:

merged_exclusion_set_sample  = conversion_flag(merged_exclusion_set_sample)

The function, however, is returning 1 for all the cases. Even if the 'conversion_month' has value 0

It would be great if someone would help me out. Thank you!

Since you are creating all these different columns, why not try this:

df['conversion_flag_oct'] = np.where((df[(df['conversion_month'] == '202010') & (df['oct2020'] == 4)]),1,0)
df['fourg_conversion_flag_nov'] = np.where((df[(df['conversion_month'] == '202011') & (df['nov2020'] == 4)]
df['fourg_conversion_flag_dec'] = np.where((df[(df['conversion_month'] == '202012') & (df['dec2020'] == 4)]),1,0)
df['fourg_conversion_flag_jan'] = np.where((df[(df['conversion_month'] == '202101') & (df['jan2021'] == 4)]),1,0)

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