Creating a new column with np.where function error and confusion

Viewed 52

I am trying to create a new column which will be named as 'P-IN.Date' in dataframe 'FinalDF'. Condition is if ['LOSS_DT (L10)(C)'] is having str value as 'NA' then return 'NA' else convert ['LOSS_DT (L10)(C)'] to appropriate date format (that is from 20200212 to 2020-02-12), But for only those values where column ['CRE group'] will be equal to "B_Ben" and column ['Mat/Sur'] will be "Other" and if the condition gets unfulfilled then simply return "Ignore".

I tried to create a nested query with code as below:

FinalDF['P-IN.Date'] = np.where(FinalDF['LOSS_DT (L10) (C)']=="<NA>","<NA>",np.where(((FinalDF['CRE group']=="B_Ben") & (FinalDF['Mat/Sur']=="Other" )),pd.to_datetime(FinalDF['LOSS_DT (L10) (C)']),"Ignore"))

If you have another approach me, Please enlighten me.

0 Answers
Related