我有两个数据框,我像这样加入它们:
merged=prvmthfile.merge(curmthfile, how='outer',on=['CUSTID','CTYPE'],suffixes=['prv','cur'],indicator=True)
现在,它将_prv和_cur添加到数据帧中的公共字段(关键字段CUSTID,CTYPE除外)。 在最后的输出中,我仅看到一组CUSTId,CTYPE,是否可以使用CUSTID_prv,CUSTID_cur和CTYPE_prv,CTYPE_Cur?
答案 0 :(得分:0)
可能只是在合并前添加后缀,然后更改合并键并删除suffix
参数:
prvmthfile.add_suffix('_prv').merge(
curmthfile.add_suffix('_cur'),
how='outer',
left_on=['CUSTID_prv', 'CTYPE_prv'],
right_on=['CUSTID_cur', 'CTYPE_cur'],
indicator=True)
示例:
import pandas as pd
df = pd.DataFrame({'id': [1,2,3,4,5],
'val': [1,2,3,4,5]})
df2 = pd.DataFrame({'id': [1,2,4,5,6],
'val': [11,22,33,44,55]})
df.add_suffix('_prv').merge(df2.add_suffix('_cur'),
how='outer',
left_on=['id_prv'],
right_on=['id_cur'],
indicator=True)
输出:
id_prv val_prv id_cur val_cur _merge
0 1.0 1.0 1.0 11.0 both
1 2.0 2.0 2.0 22.0 both
2 3.0 3.0 NaN NaN left_only
3 4.0 4.0 4.0 33.0 both
4 5.0 5.0 5.0 44.0 both
5 NaN NaN 6.0 55.0 right_only