简体   繁体   English

合并两个pandas数据框并跳过右侧的公共列

[英]merge two pandas data frame and skip common columns of right

I am using pandas DataFrame as a lightweight dataset to maintain some status and need to dynamically/continuously merge new DataFrames into existing table. 我使用pandas DataFrame作为轻量级数据集来维护某些状态,并需要动态/持续地将新DataFrame合并到现有表中。 Say I have two datasets as below: 假设我有两个数据集,如下所示:

df1: DF1:

   a  b
0  0  1
1  2  3
2  4  5
3  6  7
4  8  9

df2: DF2:

    b   c
0  10  11
1  12  13
2  14  15
3  16  17
4  18  19

I want to merge df2 to df1 (on index), and for columns in common (in this case, it is 'b'), simply discard the common column of df2. 我想将df2合并到df1(在索引上),对于共同的列(在这种情况下,它是'b'),只需丢弃df2的公共列。

   a  b   c
0  0  1  11
1  2  3  13
2  4  5  15
3  6  7  17
4  8  9  19

My code was checking common part between df1 and df2 by using SET, so that I manually drop common part in df2. 我的代码是使用SET检查df1和df2之间的公共部分,因此我手动删除df2中的公共部分。 I wonder is there any much efficient way to do this? 我想知道有没有更有效的方法来做到这一点?

First identify the columns in df2 not in df1 首先确定df2中的列而不是df1的列

cols = df2.columns.difference(df1.columns)

Then pd.DataFrame.join 然后是pd.DataFrame.join

df1.join(df2[cols])

   a  b   c
0  0  1  11
1  2  3  13
2  4  5  15
3  6  7  17
4  8  9  19

Or pd.concat will also work 或者pd.concat也可以

pd.concat([df1, df2[cols]], axis=1)

   a  b   c
0  0  1  11
1  2  3  13
2  4  5  15
3  6  7  17
4  8  9  19

Pandas merge function will also work wonders. 熊猫合并功能也将起到奇效作用。 You can do it as: 你可以这样做:

pd.merge(left=df1, right=df2, how='inner')

   a  b   c
0  0  1  11
1  2  3  13
2  4  5  15
3  6  7  17
4  8  9  19

by eliminating the 'on' attribute of merge function it will consider the columns which are in-common in both of the dataframes. 通过消除merge函数的'on'属性,它将考虑两个数据帧中共同的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM