0

With the following DF:

     A  B
0    a  1
1    b  2
2  NaN  1
3  NaN  2

I would like to replace NaN values of A based on the numeric representation of B, to get:

     A  B
0    a  1
1    b  2
2    a  1
3    b  2

I've built a dictionary of B/A values: {1 : 'a', 2: 'b'}

How can I apply the change to the NaN values?

Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186
  • nevemind, added to answer difference between using `map` and `replace` here, if in dict are all values like in `B` output is same (perfromnce the best test in real data). – jezrael Jun 09 '20 at 08:12

1 Answers1

5

Use Series.fillna with Series.map:

d = {1 : 'a', 2: 'b'}
df.A = df.A.fillna(df.B.map(d))
print (df)
   A  B
0  a  1
1  b  2
2  a  1
3  b  2

I suggest use map because replace is slowier and if no match in map is returned missing value (like original) not value of B:

df["A"] = df["A"].fillna(df["B"].replace({1 : 'a', 2: 'b'}))
print (df)
   A  B
0  a  1
1  b  2
2  a  1
3  3  3 <- changed last value to 3

d = {1 : 'a', 2: 'b'}
df.A = df.A.fillna(df.B.map(d))
print (df)
     A  B
0    a  1
1    b  2
2    a  1
3  NaN  3
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    `Series.map` is much faster than `.replace` and valid point that `map` doesn't replace values of `A` if there is no mapping. +1 – Ch3steR Jun 09 '20 at 08:16
  • @jezrael, can you please have a look here https://stackoverflow.com/questions/65090530/pandas-merge-two-dataframe-with-partial-match#65090910 ? thanks :) – Shlomi Schwartz Dec 01 '20 at 13:31