Skip to content Skip to sidebar Skip to footer

Insert Data To New Column Based On Conditions Given In Dictionary

I have the dictionary specifying the value the row should take if the conditions are met dict_map = { 'Yes' : {'Sex':'F', 'Pregnant': 'Y'}, 'No' : {'Sex':'F', 'Pregnant': '

Solution 1:

You can create the dataframe then merge

out = df.merge(pd.DataFrame(dict_map).T.reset_index(),how='left')
Out[139]: 
     ID  Sex Pregnant index
0  AB01    F        Y   Yes
1  AB02    M        N   NaN
2  AB03    M      NaN   N/A
3  AB04  NaN        Y   NaN
4  AB05    F            NaN
5  AB06    F        N    No

Post a Comment for "Insert Data To New Column Based On Conditions Given In Dictionary"