Join pandas dataframes based on column values

前端 未结 1 1315
一生所求
一生所求 2021-01-01 22:48

I\'m quite new to pandas dataframes, and I\'m experiencing some troubles joining two tables.

The first df has just 3 columns:

DF1:
item_id    positio         


        
相关标签:
1条回答
  • 2021-01-01 23:43

    I think you need merge with default inner join, but is necessary no duplicated combinations of values in both columns:

    print (df2)
       item_id  document_id col1  col2  col3
    0      337           10    s     4     7
    1     1002           11    d     5     8
    2     1003           11    f     7     0
    
    df = pd.merge(df1, df2, on=['document_id','item_id'])
    print (df)
       item_id  position  document_id col1  col2  col3
    0      337         2           10    s     4     7
    1     1002         2           11    d     5     8
    2     1003         3           11    f     7     0
    

    But if necessary position column in position 3:

    df = pd.merge(df2, df1, on=['document_id','item_id'])
    cols = df.columns.tolist()
    df = df[cols[:2] + cols[-1:] + cols[2:-1]]
    print (df)
       item_id  document_id  position col1  col2  col3
    0      337           10         2    s     4     7
    1     1002           11         2    d     5     8
    2     1003           11         3    f     7     0
    
    0 讨论(0)
提交回复
热议问题