gbhrea gbhrea - 1 year ago 181
Python Question

Pandas join dataframes by multiple key

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


Window Label FeatA
123 1 h
123 2 f


Window Label FeatB
123 1 d
123 2 s


Window Label FeatC
123 1 d
123 2 c


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

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

Answer Source

A pure pandas answer using pd.concat

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

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download