gbhrea gbhrea - 26 days ago 8
Python Question

Pandas join dataframes by multiple key

I have 3 different dataframes that I want to join, using label and window as keys.

DataFrame1

Window Label FeatA
123 1 h
123 2 f


DataFrame2

Window Label FeatB
123 1 d
123 2 s


DataFrame3

Window Label FeatC
123 1 d
123 2 c


Result

Window Label FeatA FeatB FeatC
123 1 h d d
123 2 f s c


I know how to join dataframes using
pandas.concat
but don't know how to specify keys. Any help would be greatly appreciated.

Answer

A pure pandas answer using pd.concat

pd.concat([df.set_index(['Window', 'Label']) for df in [df1_, df2_, df3_]],
          axis=1).reset_index()

enter image description here

Comments