答案1
这已得到答复这里
import pandas as pd
dict1 = {"ID": ["X\\191","R\\192","733","X215","BL000002"], "Inv": [4,4,1,3,3]}
df1 = pd.DataFrame.from_dict(dict1)
dict2 = {"ID": ["X191","X215","BL000002"], "Inv": [4,3,3]}
df2 = pd.DataFrame.from_dict(dict2)
u = df1.assign(Cnt=df1.groupby('Inv').cumcount())
v = df2.assign(Cnt=df2.groupby('Inv').cumcount())
u.merge(v, on=['Inv', 'Cnt'], how='left').drop('Cnt', 1)
ID_x Inv ID_y
0 X\191 4 X191
1 R\192 4 NaN
2 733 1 NaN
3 X215 3 X215
4 BL000002 3 BL000002
基本上,根据索引和 ID 连接两个表。