简体   繁体   English

熊猫-合并2个具有相同列名但互斥值的df

[英]Pandas - Merge 2 df with same column names but exclusive values

  • I have 1 main df MainDF, with column key and other columns not relevant. 我有1个主df MainDF,其中的列键和其他不相关的列。
    • I also have 2 other dfs, dfA and dfB, with 2 columns, key and tariff. 我也有其他2个dfs,dfA和dfB,其中包含2个列,键和关税。 The keys in dfA and dfB are exclusive, ie there is no key in both dfA and dfB. dfA和dfB中的密钥是互斥的,即dfA和dfB中都没有密钥。
    • On my MainDF, I do: MainDF.merge(dfA, how = 'left', on='key') , which will add the column "tariff" to my MainDF, for the keys in dfA and also in MainDF. 在我的MainDF上,我执行: MainDF.merge(dfA, how = 'left', on='key') ,这将在我的MainDF中添加“关税”列,用于dfA和MainDF中的键。 This will put NaN to all keys in MainDF not in dfA 这会将NaN放到MainDF中而不是dfA中的所有键中
    • Now, I need to do MainDF.merge(dfB, how = 'left', on='key') to add the tariff for the keys in MainDF but not in dfA. 现在,我需要执行MainDF.merge(dfB, how = 'left', on='key')以在MainDF中而不是在dfA中添加密钥的资费。
    • When I do the second merge, it will create in MainDF 2 columns tariff_x and tariff_y because tariff is already in MainDF following the first merge. 当我进行第二次合并时,它将在MainDF 2列中创建riff_x和riff_y,因为在第一次合并之后,关税已在MainDF中。 However, since the keys are exclusive, I need to keep only one column tariff with the not-NaN values when possible. 但是,由于键是排他的,因此我尽可能地只保留非NaN值的一栏费率。

How should I do so in a python way ? 我应该如何以python方式这样做? I could add a new column which is either tariff_x or tariff_y but I don't find that very elegant. 我可以添加一个新列,即或者riff_x或riff_y,但是我觉得那不是很优雅。

Thanks 谢谢

你可以先concat dfAdfB与合并前MainDF

MainDF.merge(pd.concat([dfA, dfB], axis=0), how='left', on='key')

Do you need something like this: 您是否需要以下内容:

dfA = pd.DataFrame({'tariff': [1, 2, 3], 'A': list('abc')})
dfB = pd.DataFrame({'tariff': [4, 5, 6], 'A': list('def')})

dfJoin = pd.concat([dfA, dfB], ignore_index=True)

     A    B  tariff
0    a  NaN       1
1    b  NaN       2
2    c  NaN       3
3  NaN    d       4
4  NaN    e       5
5  NaN    f       6

Now you can merge with dfJoin . 现在,您可以与dfJoin合并。

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

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