I have a pd dataframe which includes the columns CompTotal and CompFreq.
I wanted to add a third column- NormalizedAnnualCompensation which uses the following logic If the CompFreq is Yearly then use the exising value in CompTotal If the CompFreq is Monthly then multiply the value in CompTotal with 12 If the CompFreq is Weekly then multiply the value in CompTotal with 52
I eventually used np.where() to basically write a nested if statement like I'm used to bodging together in excel(pretty new to coding in general)- that's below.
My question is- Could I have done it better? This doesn't feel very pythonic based on what I've read and what I've been taught so far.
df['NormalizedAnnualCompensation'] = np.where(df['CompFreq']=='Yearly',df.CompTotal,
(np.where(df['CompFreq']=='Monthly', df.CompTotal * 12,
(np.where(df['CompFreq']=='Weekly',df.CompTotal *52,'NA')
))))
Thanks in advance.