Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas join dataframes by multiple key [duplicate]

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.

like image 222
gbhrea Avatar asked Mar 12 '23 15:03

gbhrea


1 Answers

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

like image 192
piRSquared Avatar answered Mar 23 '23 02:03

piRSquared