【问题标题】:Implementing cdc but getting value error in Python Pandas实现 cdc 但在 Python Pandas 中出现值错误
【发布时间】:2019-11-06 13:21:08
【问题描述】:

我正在尝试通过 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)

这不起作用。 以下是错误信息:

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

增量文件:

 cust_id cust_name  cust_income cust_phone
0      222     b        20000          jio
1      333     c        120000     iphoneX
2      666     f        76000      oneplus

预期输出:

   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

【问题讨论】:

  • 如果您提供一些输入数据和所需的输出会很有帮助
  • 我的错,我已经更新了问题并添加了示例和预期输出

标签: python pandas dataframe change-data-capture


【解决方案1】:

appenddrop_duplicateskeep='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

【讨论】:

    【解决方案2】:

    使用DataFrame.merge + DataFrame.drop_duplicates:

    new_df=( df_master.merge(df_delta,how='outer',sort=False)
                      .drop_duplicates(['cust_name','cust_phone'],keep='last')
                      .sort_values('cust_id')
                      .reset_index(drop=True) )
    print(new_df)
    
       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
    

    pd.concat:

    new_df=(pd.concat([df_master,df_delta],sort=False)
              .drop_duplicates(['cust_name','cust_phone'],keep='last')
              .sort_values('cust_id')
              .reset_index(drop=True) )
    print(new_df)
    
       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
    

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多