self-join with Pandas

后端 未结 3 1926
小鲜肉
小鲜肉 2021-01-05 03:42

I would like to perform a self-join on a Pandas dataframe so that some rows get appended to the original rows. Each row has a marker \'i\' indicating which row should get ap

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-05 04:16

    Try this:

    In [69]: d.join(d.set_index('i'), rsuffix='_y')
    Out[69]:
      some_col  i some_col_y
    0        A  2        NaN
    1        B  1          B
    1        B  1          C
    2        C  1          A
    

    or:

    In [64]: pd.merge(d[['some_col']], d, left_index=True, right_on='i', suffixes=['_y','']).sort_index()
    Out[64]:
      some_col_y some_col  i
    0          C        A  2
    1          B        B  1
    2          B        C  1
    

提交回复
热议问题