实现 cdc 但在 Python Pandas 中出现值错误

aka*_*rma 1 python change-data-capture dataframe pandas

我正在尝试通过 Python 执行 CDC 操作。我正在尝试将未更改的数据(主文件/基表)与新文件(增量文件)合并。

下面是我写的函数:

def processInputdata():
    df1 = pd.read_csv('master.csv')
    df2 = pd.read_csv('delta.csv')
    df=pd.merge(df1,df2,on=['cust_id','cust_id'],how="outer",indicator=True)
    dfo=df[df['_merge']=='left_only']
    dfT =pd.merge(dfo,df2,on=['cust_id','cust_id'],how="right",indicator=True)
Run Code Online (Sandbox Code Playgroud)

这是行不通的。下面是错误信息:

ValueError: 不能为指标列使用现有列的名称

我不确定是否有任何更简单或更好的方法来执行 CDC。

样本数据 :

主文件 :

   cust_id cust_name  cust_income cust_phone
0      111     a            78000       sony
1      222     b             8000        jio
2      333     c           108000     iphone
3      444     d           200000    iphoneX
4      555     e            20000    samsung
Run Code Online (Sandbox Code Playgroud)

增量文件:

 cust_id cust_name  cust_income cust_phone
0      222     b        20000          jio
1      333     c        120000     iphoneX
2      666     f        76000      oneplus
Run Code Online (Sandbox Code Playgroud)

预期输出:

   cust_id cust_name  cust_income cust_phone
0      111     a            78000       sony
1      222     b            20000        jio
2      333     c           120000     iphoneX
3      444     d           200000    iphoneX
4      555     e            20000    samsung
5.     666     f           76000     oneplus
Run Code Online (Sandbox Code Playgroud)

Erf*_*fan 5

使用appenddrop_duplicates具有keep='last'

df = master.append(delta)\
           .drop_duplicates(subset=['cust_id','cust_phone'], keep='last')\
           .sort_values('cust_name').reset_index(drop=True)

   cust_id cust_name  cust_income cust_phone
0      111         a        78000       sony
1      222         b         8000        jio
2      333         c       108000    iphoneX
3      444         d       200000    iphoneX
4      555         e        20000    samsung
5      666         f        76000    oneplus
Run Code Online (Sandbox Code Playgroud)