【问题标题】:Merging two data frames not getting Key Column on both dataframes with Full OuterJoin使用完全外连接合并两个数据帧在两个数据帧上没有获得关键列
【发布时间】:2020-10-08 12:08:28
【问题描述】:

我有两个如下所示的数据框。我使用 pandas 和 numpy 来比较差异。

df_a
               Key                           Value
0       data_owner                            John
1     locationcode                           local
2             Unit                           sales
3      application                       autosales


df_b 
               Key                           Value
0       data_owner                            John
1     locationcode                           local
2             Unit                           sales
3      application                       autosales
4       department                     frontoffice

我正在使用以下代码进行合并

 df = pd.merge(df_a,df_b,on=['Key'],how='outer',left_index=True,right_index=True)

 df['diff'] = np.where((df['Value_x']==df['Value_y']), 'No', 'Yes')

我打算输出比较 df 和两边任何缺失的项目。

下面的实际输出:但问题是我想从两个数据帧中显示 Key,但如果你看到下面的输出它只显示一次,即我需要 Key_y 也成为输出的一部分。

              Key                         Value_x                          Value_y   diff
0       data_owner                            John                            John   No
1     locationcode                           local                           local   No
2             unit                           sales                           sales   No
3      application                       autosales                       autosales   No
4       department                     frontoffice                             NaN   No

预期输出:我想同时显示 Key

            Key_x                          Value_x       Key_y                    Value_y    diff
0       data_owner                            John       data_owner                  John    No
1     locationcode                           local       locationcode               local    No
2             unit                           sales       unit                       sales    No
3      application                       autosales       application            autosales    No
4       department                     frontoffice       NaN                          NaN    Yes

【问题讨论】:

    标签: python pandas dataframe outer-join


    【解决方案1】:

    使用DataFrame.add_suffix在合并前将后缀添加到两个数据框的列中,这样它们的键在合并后不会合并为单个列:

    df = pd.merge(
        df_b.add_suffix('_x'), df_a.add_suffix('_y'), 
        left_on='Key_x', right_on='Key_y', how='outer')
    
    df['diff'] = np.where(df['Value_x'].eq(df['Value_y']), 'No', 'Yes')
    

    # print(df)
              Key_x      Value_x         Key_y    Value_y diff
    0    data_owner         John    data_owner       John   No
    1  locationcode        local  locationcode      local   No
    2          Unit        sales          Unit      sales   No
    3   application    autosales   application  autosales   No
    4    department  frontoffice           NaN        NaN  Yes
    

    【讨论】:

    • 它就像魅力一样。谢谢.. 还有一个疑问如何获得“不适用”而不是 Nan。
    • 只需使用df = df.fillna('Not Applicable')
    猜你喜欢
    • 2020-06-10
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2020-04-07
    • 1970-01-01
    相关资源
    最近更新 更多