如何连接两个ID不匹配的数据框,并创建新列以表示ID来自何数据框?

时间:2018-11-29 14:34:28

标签: python python-3.x pandas dataframe

我有两个这样的数据框

df1:

id    column1    column2 
1      30          90
2      1            2

df2:

id    column1    column2 
1      30          90
3      1            2

我想创建逻辑来合并ID不匹配(列名相同)的这两个数据框,然后我想创建一个新列来说明ID来自何数据框。我该怎么做?

最终合并的df:

id    column1    column2    df_name
2      30          90         df1
3      1            2         df2

编辑:

最终df能否从两个数据框中提取所有列?

 id    column1.df1    column2.df1   column1.df2    column2.df2     df_name
    2      30          90                 30            90           df1
    3      1            2                  1             2           df2

2 个答案:

答案 0 :(得分:3)

第一个concat数据帧在一起:

df = (pd.concat([df1, df2],  keys=('df1','df2'))
        .rename_axis(('df_name','idx'))
        .reset_index(level=1, drop=True)
        .reset_index())

print (df)
  df_name  id  column1  column2
0     df1   1       30       90
1     df1   2        1        2
2     df2   1       30       90
3     df2   3        1        2

然后得到相同的id

a = df1.merge(df2, on='id')['id']

最后用isin进行过滤:

df = df[~df['id'].isin(a)]
print (df)
  df_name  id  column1  column2
1     df1   2        1        2
3     df2   3        1        2

编辑:

类似于@ W-B的解决方案,只添加了参数idsuffixes

df = (df1.merge(df2,indicator=True,how='outer', on='id', suffixes=('_df1','_df2'))
         .query("_merge != 'both'"))
df['_merge'] = df['_merge'].map({'left_only':'df1','right_only':'df2'})

print (df)
   id  column1_df1  column2_df1  column1_df2  column2_df2 _merge
1   2          1.0          2.0          NaN          NaN    df1
2   3          NaN          NaN          1.0          2.0    df2

如果要所有行,也要使用相同的id行:

df['_merge'] = df['_merge'].map({'left_only':'df1','right_only':'df2', 'both':'df1+df2'})

print (df)
   id  column1_df1  column2_df1  column1_df2  column2_df2   _merge
0   1         30.0         90.0         30.0         90.0  df1+df2
1   2          1.0          2.0          NaN          NaN      df1
2   3          NaN          NaN          1.0          2.0      df2

答案 1 :(得分:2)

让我们使用merge

df=df1.merge(df2,indicator = True,how='outer').loc[lambda x : x['_merge'].ne('both')]
df['df_name']=df['_merge'].map({'left_only':'df1','right_only':'df2'})
df
Out[328]: 
   id  column1  column2      _merge df_name
1   2        1        2   left_only     df1
2   3        1        2  right_only     df2