Pandas: map dictionary values on an existing column based on key from another column to replace NaN

Jonas_Konig :

I've had a good look and I can't seem to find the answer to this question. I am wanting to replace all NaN values in my Department Code Column of my DataFrame with values from a dictionary, using the Job Number column as the Key matching that of the dictionary. The data can be seen Below: Please note there are many extra columns, these are just the two.)

df = 
       Job Number Department Code
    0      3525             403
    1      4555             NaN
    2      5575             407
    3      6515             407
    4      7525             NaN
    5      8535             102
    6      3545             403
    7      7455             102
    8      3365             NaN
    9      8275             403
    10     3185             408

dict = {'4555': '012', '7525': '077', '3365': '034'}

What I am hoping the output to look like is:

       Job Number Department Code
    0      3525             403
    1      4555             012
    2      5575             407
    3      6515             407
    4      7525             077
    5      8535             102
    6      3545             403
    7      7455             102
    8      3365             034
    9      8275             403
    10     3185             408

The two columns are object datatypes and I have tried the replace function which I have used before but that only replaces the value if the key is in the same column.

df['Department Code'].replace(dict, inplace=True)

This does not replace the NaN values.

I'm sure the answer is very simple and I apologies in advance but i'm just stuck.

(Excuse my poor code display, it's handwritten as not sure how to export code from python to here.)

jezrael :

Better is avoid variable dict, because builtin (python code word), then use Series.fillna for replace matched values with Series.map, if no match values return NaN, so no replacement:

d = {'4555': '012', '7525': '077', '3365': '034'}
df['Department Code'] = df['Department Code'].fillna(df['Job Number'].astype(str).map(d))
print (df)
    Job Number Department Code
0         3525             403
1         4555             012
2         5575             407
3         6515             407
4         7525             077
5         8535             102
6         3545             403
7         7455             102
8         3365             034
9         8275             403
10        3185             408

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216111&siteId=1