在pandas join中复制iferror和vlookup

时间:2016-10-11 09:52:59

标签: python pandas join merge

我想加入两个数据帧:

df1 = pd.DataFrame({'Banner': {0: 'banner1', 1: 'banner2', 2: 'banner3'},
                    'Campaign': {0: 'campaign1', 1: 'campaign2', 2: '12345'},
                    'Country ': {0: 'de', 1: 'it', 2: 'de'},
                    'Date': {0: '1/1/2016', 1: '2/1/2016', 2: '1/1/2016'},
                    'Value_1': {0: 10, 1: 5, 2: 20}})

df2 = pd.DataFrame({'Banner': {0: 'banner1', 1: 'banner2', 2: 'banner3', 3: 'banner4', 4: 'banner5'},
                    'Campaign': {0: 'campaign1',1: 'campaign2', 2: 'none',3: 'campaign4',4: 'campaign5'},
                    'Country ': {0: 'de', 1: 'it', 2: 'de', 3: 'en', 4: 'en'},
                    'Date': {0: '1/1/2016', 1: '2/1/2016', 2: '1/1/2016', 3: '3/1/2016', 4: '4/1/2016'},
                    'Value_2': {0: 5, 1: 10, 2: 15, 3: 20, 4: 25},              
                    'id_campaign': {0: 'none', 1: 'none', 2: '12345', 3: 'none', 4: 'none'}})

修改: 我们甚至想象一下这个选项:

df1 = pd.DataFrame({'Banner': {0: 'banner1', 1: 'banner2', 2: 'banner3'},
                'Campaign': {0: 'campaign1', 1: 'campaign2', 2: '12345'},
                'Date': {0: '1/1/2016', 1: '2/1/2016', 2: '1/1/2016'},
                'Value_1': {0: 10, 1: 5, 2: 20}})

我必须在键上加入df2和df1:

  • 日期
  • 运动
  • 横幅

这里的问题是,当找不到“Campaign”键下的匹配时,键应切换到“id_campaign”字段。

我想获取此数据框:

df_joined = pd.DataFrame({'Banner': {0: 'banner1', 1: 'banner2', 2: 'banner3', 3: 'banner4', 4: 'banner5'},
                      'Campaign': {0: 'campaign1', 1: 'campaign2', 2: 'none', 3: 'campaign4', 4: 'campaign5'},
                      'Country ': {0: 'de', 1: 'it', 2: 'de', 3: 'en', 4: 'en'},
                      'Date': {0: '1/1/2016', 1: '2/1/2016', 2: '1/1/2016', 3: '3/1/2016', 4: '4/1/2016'},
                      'Value_1': {0: 10, 1: 5, 2: 20, 3: 0, 4: 0},
                      'Value_2': {0: 5, 1: 10, 2: 15, 3: 20, 4: 25}, 
                      'id_campaign': {0: 'none', 1: 'none', 2: '12345', 3: 'none', 4: 'none'}})

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用双merge乘3和2键,然后在Value_1的{​​{1}}列combine_first填充不匹配值:

df4