Issue
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!
Solution
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)
Answered By - Serge de Gosson de Varennes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.