I would like to do a self-join on the Pandas framework so that some lines are added to the original lines. Each line has an “i” token indicating which line should be attached to it to the right.
d = pd.DataFrame(['A','B','C'], columns = ['some_col']) d['i'] = [2,1,1] In [17]: d Out[17]: some_col i 0 A 2 1 B 1 2 C 1
Required Conclusion:
some_col i some_col_y 0 A 2 C 1 B 1 B 2 C 1 B
That is, line 2 is added to line 0, line 1 to line 1, line 1 to line 2 (as indicated by i).
My idea of how to do this was
pd.merge(d, d, left_index = True, right_on = 'i', how = 'left')
But it produces something completely different. How to do it right?
source share