1

I am trying to concatenate two data frames with

pd.concat([df1.set_index(["t", "tc"]), df2.set_index(["t", "tc"])], axis=1)

It can happen that in df1, the index is not unique. In that case, I want the corresponding entry in df2 to be inserted into all the rows with that index. Unfortunately, instead of doing that, concat gives me an error.I thought ignore_index = True might help, but I still get the error ValueError: cannot handle a non-unique multi-index! Is there an alternative to concat that does what I want?

For example: df1

t  tc a
a  1  5
b  1  6
a  1  7

df2:

t tc b
a 1  8
b 1  10

result(after resetting the index):

t tc a b
a 1  5 8
b 1  6 10
a 1  7 8
LizzAlice
  • 678
  • 7
  • 18
  • 1
    can you post the sample data and expected output – deadshot Aug 27 '20 at 14:53
  • Yes, merge, append: https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html Actually concat should work, just need to set the axis on which to do that. – Petronella Aug 27 '20 at 15:01
  • Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – sushanth Aug 27 '20 at 15:02

1 Answers1

2

using .merge you can get where you need

df1.merge(df2, on =['t', 'tc'])
#result
    t   tc  a   b
0   a   1   5   8
1   a   1   7   8
2   b   1   6   10
Terry
  • 2,761
  • 2
  • 14
  • 28