1

I have been trying to concatenate two MultiIndex together, but for some reason it has not worked out yet...

import numpy as np
import pandas as pd

df = pd.DataFrame([[1,1,0,0,4],
                   [1,1,1,0,8],
                   [1,1,2,0,6],
                   [2,1,0,0,4],
                   [2,1,1,0,3]], columns=['a', 'b', 'c', 'd', 'e']
df2 = pd.DataFrame([[1,1,0,2,4],
                    [2,1,1,2,3]], columns=['a', 'b', 'c', 'd', 'e']

df = df.set_index(['a', 'b', 'c'])
df2 = df2.set_index(['a', 'b', 'c'])

df = pd.concat([df,df2], axis=1, join='inner')

This is the way I tried to do it and I really thought this should work. Can anyone maybe help to figure out how to combine these two in order to just get the rows where columns a, b, and c match.

The result I'm looking for:

      d_x e_x d_y e_y
a b c
1 1 0  0   4   2   4
2 1 1  0   3   2   3

1 Answers1

1

Use merge instead of concatenate:

df.merge(df2, left_index=True, right_index=True)
Toukenize
  • 1,310
  • 1
  • 5
  • 11