Search code examples
pythonpandasdataframedata-sciencedata-munging

Python pandas dataframe merge 2 rows into one with keys


I have 2 dataframe, each has a single row, with the same columns:

df1 = feat_1 feat_2 ... feat_n a b c d ... z
          A    B          N    1 2 3 4     9 

df2 = feat_1 feat_2 ... feat_n a b c d ... z
          A    B          N    5 6 1 8     3

feat_1.. feat_n are the same with the 2 rows and the other columns should be concatenated such that the new row will be:

new_df = feat_1 feat_2 ... feat_n a_1 a_2 b_1 b_2 c_1 c_2 d_1 d_2 ... z_1 z_2
          A    B             N     1   5   2   6   3   1   4   8       9   3

What is the best way to do it?


Solution

  • Just use the merge command:

    new_df = pd.merge(df1, df2, how="inner", on =["feat_1", 'feat_2', 'feat_n'], suffixes=('_1','_2'))
    

    Input:

    df1
      feat_1 feat_2 feat_n  a  b
    0      A      B      N  1  2
    
    df2
      feat_1 feat_2 feat_n  a  b
    0      A      B      N  3  4
    

    Code:

    new_df = pd.merge(df1, df2, how="inner", on =["feat_1", 'feat_2', 'feat_n'], suffixes=('_1','_2'))
    

    Prints:

      feat_1 feat_2 feat_n  a_1  b_1  a_2  b_2
    0      A      B      N    1    2    3    4