熊猫合并两个数据框并覆盖行

时间:2019-10-03 20:32:01

标签: python-3.x pandas

我有两个要合并的数据框-

数据框1-

           Product     Buyer       Date       Store
                TV  Person A  9/18/2018      Boston
               DVD  Person B  4/10/2018    New York
   Blue-ray Player  Person C  9/19/2018      Boston
             Phone  Person A  9/18/2018      Boston
      Sound System  Person C  3/05/2018  Washington

数据框2-

  Product Type     Buyer       Date       Store
            TV  Person B  5/29/2018    New York
         Phone  Person A  2/10/2018  Washington

第一个数据帧大约有50万行,而第二个数据帧大约有8万行。有时第二个数据框具有主列,但我试图获得最终输出,以显示与数据框1相同的列,并使用数据框2更新数据框1的行。

输出看起来像这样-

           Product     Buyer       Date       Store
                TV  Person B  5/29/2018    New York
               DVD  Person B  4/10/2018    New York
   Blue-ray Player  Person C  9/19/2018      Boston
             Phone  Person A  2/10/2018  Washington
      Sound System  Person C  3/05/2018  Washington

我尝试了联接,但是重复了这些列。有一个优雅的解决方案吗?

编辑1- 我已经尝试过-

pd.merge(df,df_correction, left_on = ['Product'], right_on = ['Product Type'],how = 'outer')



     Product   Buyer_x     Date_x     Store_x Product Type   Buyer_y     Date_y     Store_y
                TV  Person B  5/29/2018    New York           TV  Person B  5/29/2018    New York
               DVD  Person B  4/10/2018    New York          NaN       NaN        NaN         NaN
   Blue-ray Player  Person C  9/19/2018      Boston          NaN       NaN        NaN         NaN
             Phone  Person A  2/10/2018  Washington        Phone  Person A  2/10/2018  Washington
      Sound System  Person C  3/05/2018  Washington          NaN       NaN        NaN         NaN

1 个答案:

答案 0 :(得分:1)

我认为组合首先是您要寻找的功能https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.combine_first.html

您可以尝试:

$ awk 'BEGIN{a=1/3; a=a""; print a}' 0.333333 $ awk 'BEGIN{OFMT="%.20g"; a=1/3; a=a""; print a }' 0.333333 $ awk 'BEGIN{CONVFMT="%.20g"; a=1/3; a=a""; print a }' 0.33333333333333331483

enter image description here