我有两个数据框:
Dataframe_A:
Account_Nbr Customer_ID Gender
1234 A1234 male
5678 ? female
Dataframe_B:
Account_Nbr Customer_ID
1234 A1234
5678 B5678
我想替换为'?'在带有“ B5678”的数据框A中,这是我的代码:
Dataframe_A = Dataframe_A.assign(
Customer_ID = lambda x:
[cid if (cid != '?' ) else
Datafram_B.loc[Datafram_B['Account_Nbr'] == acct, ['Customer_ID']]
for cid, acct in zip(x.Customer_ID, x.Account_Nbr)]
Dataframe_A
但是输出不是我期望的:
Account_Nbr Customer_ID Gender
1234 A1234 male
5678 Customer_ID female
B5678
好像它用整个系列取代了电池。我如何获得这样的输出?谢谢。
Account_Nbr Customer_ID Gender
1234 A1234 male
5678 B5678 female
答案 0 :(得分:0)
下面的代码应该可以完成工作。
import pandas as pd
df1 = pd.DataFrame([
[1234, 'A1234', 'male'],
[5678, '?', 'female']], columns=['Account_Nbr', 'Customer_ID', 'Gender'])
df2 = pd.DataFrame([
[1234, 'A1234'],
[5678, 'B5678']], columns=['Account_Nbr', 'Customer_ID'])
mask = df1['Account_Nbr'] == df2['Account_Nbr']
df1.loc[mask, 'Customer_ID'] = df2[mask]['Customer_ID']
df1.head()
输出:
Account_Nbr Customer_ID Gender
0 1234 A1234 male
1 5678 B5678 female