以下是我的2个pandas数据帧的小样本:
In [65]: df1
Out[65]:
Send_Agent Send_Amount Country_Code
0 AWD120279 85.99 KW
1 API185805 22.98 PH
2 ANO080012 490.00 NO
3 AUK359401 616.16 GB
4 ACL000105 193.78 CL
In [44]: df2
Out[44]:
Country_Code Rating
0 KW Medium
1 PH Higher
2 NO Lower
3 GB Lower
4 CL Lower
In [97]: df4 = df1[df1['Send_Agent']=='AWD120279']
In [98]: df4
Out[98]:
Send_Agent Send_Amount Country_Code
0 AWD120279 85.99 KW
3359 AWD120279 200.00 KW
3878 AWD120279 203.03 KW
In [102]: df5 = df2[df2['Country_Code']=='KW']
In [104]: df5
Out[104]:
Country_Code Rating
15 KW Medium
In [105]: pd.merge(df4,df5,on='Country_Code',how='left')
Out[105]:
Send_Agent Send_Amount Country_Code Rating
0 AWD120279 85.99 KW NaN
1 AWD120279 200.00 KW NaN
2 AWD120279 203.03 KW NaN
我无法弄清楚为什么“评级”列在合并后会转换为Nan。每个Country_Code都有与之关联的评级。所以,它永远不应该是南。