I have a DataFrame which has job numbers and the customer names associated with that job. 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.
This is the original DataFrame (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:
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))
.
I wasn't sure how to paste code into here so I have written out the df and series by hand.
Any help would be greatly appreciated.
You could use reset_index
with combine_first
:
(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
:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.