Joining two pandas dataframes based on multiple conditions

前端 未结 2 1785
滥情空心
滥情空心 2021-01-05 00:54

df_a and df_b are two dataframes that looks like following

df_a
A   B       C      D     E
x1  Apple   0.3   0.9    0.6
x1  Orange          


        
相关标签:
2条回答
  • 2021-01-05 01:07

    You need an inner merge, specifying both merge columns in each case:

    res = df_a.merge(df_b, how='inner', left_on=['A', 'B'], right_on=['A', 'B_new'])
    
    print(res)
    
        A       B    C    D    E   B_new    F
    0  x1   Apple  0.3  0.9  0.6   Apple  0.3
    1  x1  Orange  0.1  0.5  0.2  Orange  0.1
    2  x2   Apple  0.2  0.2  0.1   Apple  0.2
    3  x2  Orange  0.3  0.4  0.9  Orange  0.3
    4  x2   Mango  0.1  0.2  0.3   Mango  0.1
    5  x3  Orange  0.3  0.1  0.2  Orange  0.3
    
    0 讨论(0)
  • 2021-01-05 01:22

    You can still achieve this with a left join which is very ideal.
    See below:

    final_df = pd.merge(df_a, df_b[['A', 'B_new','F']], how="left", left_on=['A', 'B'], right_on=['A', 'B_new']);
    
    0 讨论(0)
提交回复
热议问题