[英]Pandas: map dictionary values on an existing column based on key from another column to replace NaN
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.
我想用字典中的值替换我的 DataFrame 部门代码列中的所有 NaN 值,使用 Job Number 列作为匹配字典的键。 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.这不会替换 NaN 值。
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.) (请原谅我糟糕的代码显示,它是手写的,因为不确定如何将代码从 python 导出到这里。)
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:更好的是避免变量
dict
,因为builtin
(Python代码字),然后用Series.fillna
用于替换匹配的值Series.map
,如果没有匹配的值返回NaN
,所以没有更换:
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
Or another way is using set_index
and fillna
:或者另一种方法是使用
set_index
和fillna
:
df['Department Code'] = (df.set_index('Job Number')['Department Code']
.fillna(d).values)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.