Pandas: Concatenate dataframe and keep duplicate indices

后端 未结 1 940

I have two dataframes that I would like to concatenate column-wise (axis=1) with an inner join. One of the dataframes has some duplicate indices, but the rows are not duplicates

1条回答
  •  一向
    一向 (楼主)
    2021-02-13 20:22

    You can perform a merge and set the params to use the index from the lhs and rhs:

    In [4]:    
    df1.merge(df2, left_index=True, right_index=True)
    Out[4]:
       b  c
    a      
    1  2  5
    1  3  5
    2  4  6
    
    [3 rows x 2 columns]
    

    Concat should've worked, it worked for me:

    In [5]:
    
    pd.concat([df1,df2], join='inner', axis=1)
    Out[5]:
       b  c
    a      
    1  2  5
    1  3  5
    2  4  6
    
    [3 rows x 2 columns]
    

    0 讨论(0)
提交回复
热议问题