简体   繁体   English

Pandas:根据另一列的键在现有列上映射字典值以替换 NaN

[英]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_indexfillna

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.

相关问题 Pandas:如何根据另一列替换列中的 Nan 值? - Pandas: How to replace values of Nan in column based on another column? pandas Dataframe基于键列,将NaN值替换为以前的值 - pandas Dataframe Replace NaN values with with previous value based on a key column 如何根据字典键和值替换熊猫数据框列值? - How to replace pandas dataframe column values based on dictionary key and values? 使用另一列上的键和字典中的值替换熊猫列值 - Replace pandas column values using a key on another column and value from a dictionary 如何根据另一列中的值用另一列的平均值替换 NaN 值? Pandas - How to replace NaN values with another column's mean based on value in another column? Pandas 根据 pandas 中字典中另一列的值添加新列 - Add new column based on values of another column from a dictionary in pandas Pandas:根据来自另一列的匹配替换列值 - Pandas: replace column values based on match from another column 如何根据另一列中的值有条件地替换一列中的 NaN 值 - How to conditionally replace NaN values in a column based on values in another column 根据字典(熊猫)替换列中的值 - Replace values in a column based on a dictionary (Pandas) Pandas Dataframe 用 B 列的值替换 A 列的 NaN 值 - Pandas Dataframe replace NaN values of column A with values from column B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM