self-join with Pandas

后端 未结 3 1953
小鲜肉
小鲜肉 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:19

    Instead of using merge you can also use indexing and assignment:

    >>> d['new_col'] = d['some_col'][d['i']].values
    >>> d
      some_col  i new_col
    0        A  2       C
    1        B  1       B
    2        C  1       B
    

提交回复
热议问题