[英]Mapping values from series over a column to replace nan values pandas
I have a DataFrame which has job numbers and the customer names associated with that job.我有一个 DataFrame,它有工作编号和与该工作相关的客户名称。 There are instances where the job numbers have no customer name and therefore is null.
在某些情况下,作业编号没有客户名称,因此为空。 I have a separate series which has these job numbers as index and the missing customer names to replace the null values, based on the job numbers.
我有一个单独的系列,其中将这些工作编号作为索引,并根据工作编号将缺失的客户名称替换为空值。 I am not entirely sure how I would go about mapping this over the original DataFrame column.
我不完全确定我将如何将其映射到原始 DataFrame 列上。
This is the original DataFrame (df):这是原始数据帧 (df):
Job Number Customer
0 02123 Paul F
1 46456 nan
2 56823 Kevin T
3 62948 nan
The series to replace the nan values:替换 nan 值的系列:
Job Number
46456 Kara L
62948 Sabrina M
Name: Customers, dtype: object
The final output I need is:我需要的最终输出是:
Job Number Customer
0 02123 Paul F
1 46456 Kara L
2 56823 Kevin T
3 62948 Sabrina M
I hope this makes sense.我希望这是有道理的。 I have had a look at other answers such as using:
df['Customer'] = df['Job Number'].map(customers)
but that didn't work or test['Customer'] = df['Customer'].combine_first(df['Customer'].map(customers))
.我查看了其他答案,例如使用:
df['Customer'] = df['Job Number'].map(customers)
但这不起作用或test['Customer'] = df['Customer'].combine_first(df['Customer'].map(customers))
。
I wasn't sure how to paste code into here so I have written out the df and series by hand.我不确定如何将代码粘贴到这里,所以我手工写出了 df 和 series。
Any help would be greatly appreciated.任何帮助将不胜感激。
You could use reset_index
with combine_first
:您可以将
reset_index
与combine_first
reset_index
使用:
(df.set_index('JobNumber').squeeze()
.combine_first(customers.set_index('Job').squeeze())
.reset_index())
index Customer
0 2123 Paul F
1 46456 Kara L
2 56823 Kevin T
3 62948 Sabrina M
Here is necessary use map
by column Job Number
instead Customer
:这里有必要按列
Job Number
使用map
而不是Customer
:
df['Customer'] = df['Customer'].combine_first(df['Job Number'].map(customers))
print (df)
Job Number Customer
0 2123 Paul F
1 46456 Kara L
2 56823 Kevin T
3 62948 Sabrina M
Detail :详情:
print (df['Job Number'].map(customers))
0 NaN
1 Kara L
2 NaN
3 Sabrina M
Name: Job Number, dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.