1

How to merge (left join) using column value from dataframe A and index of dataframe B?

For example:

>>> A              >>> B
    lkey value         rkey value
0   foo  1         0   foo  5
1   bar  2         1   bar  6
2   baz  3         2   qux  7
3   foo  4         3   bar  8

to get:

   lkey  value_x  rkey  value_y
 0 foo   1        bar   6
 1 bar   2        qux   7
 2 baz   3        bar   8
 3 foo   4        NaN   NaN
smci
  • 32,567
  • 20
  • 113
  • 146
DevEx
  • 4,337
  • 13
  • 46
  • 68
  • The question [Pandas merge on index column?](https://stackoverflow.com/questions/45889486/pandas-merge-on-index-column) is a duplicate of this (albeit less clearly-written). I suggest this should be the canonical. – smci Apr 19 '18 at 07:57

1 Answers1

5

try using left_on and right_index to do the merging, like:

m = pd.merge(dfA, dfB, right_index = True, left_on='value')
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162