Combine data frames with duplicate indexes

Is there a way to merge two dataframes while one of them has duplicate indices like:

dataframe A:

     value    
 key
  a    1
  b    2
  b    3
  b    4
  c    5
  a    6

      

dataframe B:

       number
  key
   a     I
   b     X
   c     V

      

after the merge, I want to have a dataframe like below:

       value      number
  key
   a     1          I
   b     2          X
   b     3          X
   b     4          X
   c     5          V
   a     6          I

      

Or maybe there are better ways to do this with groupby?

+3


source to share


2 answers


>>> a.join(b).sort('value')
     value number
key              
a        1      I
b        2      X
b        3      X
b        4      X
c        5      V
a        6      I

      



+1


source


Use join :



>>> a = pd.DataFrame(range(1,7), index=list('abbbca'), columns=['value'])
>>> b = pd.DataFrame(['I', 'X', 'V'], index=list('abc'), columns=['number'])
>>> a.join(b)
   value number
a      1      I
a      6      I
b      2      X
b      3      X
b      4      X
c      5      V

      

+1


source







All Articles