Pandas: fill in NaN values with dictionary references another column

后端 未结 3 1610
挽巷
挽巷 2020-12-05 20:07

I have a dictionary that looks like this

dict = {\'b\' : \'5\', \'c\' : \'4\'}

My dataframe looks something like this

   A  B
0          


        
3条回答
  •  無奈伤痛
    2020-12-05 21:06

    This can be done simply

    df['B'] = df['B'].fillna(df['A'].apply(lambda x: dict.get(x)))
    

    This can work effectively for a bigger dataset as well.

提交回复
热议问题