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?

1
  • 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). Commented Jun 9, 2020 at 8:12

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Series.map is much faster than .replace and valid point that map doesn't replace values of A if there is no mapping. +1
@jezrael, can you please have a look here stackoverflow.com/questions/65090530/… ? thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.