4

I have a dataframe df something like this

A     B      C
1     'x'    15.0
2     'y'    NA
3     'z'    25.0

and a dictionary dc something like

dc = {'x':15,'y':35,'z':25}

I want to fill all nulls in column C of the dataframe using values of column B from the dictionary. So that my dataframe will become

A     B      C
1     'x'    15
2     'y'    35
3     'z'    25

Could anyone help me how to do that please?

thanks, Manoj

DYZ
  • 55,249
  • 10
  • 64
  • 93
Manoj Agrawal
  • 775
  • 3
  • 8
  • 20

2 Answers2

7

You can use fillna with map:

dc = {'x':15,'y':35,'z':25}

df['C'] = df.C.fillna(df.B.map(dc))
df
#   A   B      C
#0  1   x   15.0
#1  2   y   35.0
#2  3   z   25.0
Psidom
  • 209,562
  • 33
  • 339
  • 356
3
df['C'] = np.where(df['C'].isnull(), df['B'].apply(lambda x: dc[x]), df['C'])
DYZ
  • 55,249
  • 10
  • 64
  • 93