LizzAlice
LizzAlice

Reputation: 720

Alternative to pandas concat

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

Upvotes: 1

Views: 4326

Answers (1)

Terry
Terry

Reputation: 2811

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

Upvotes: 2

Related Questions