Pandas join dataframes by multiple key [duplicate] Pandas join dataframes by multiple key [duplicate] pandas pandas

Pandas join dataframes by multiple key [duplicate]


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


You need to use the merge function for joining tables, for your case, since you have multiple data frames to join, you can put them into a list and then use the reduce from functools to merge them one by one:

import pandas as pdfrom functools import reducereduce(lambda x, y: pd.merge(x, y, on = ['Window', 'Label']), [df1, df2, df3])#  Window   Label   FeatA   FeatB   FeatC# 0   123       1       h      d        d# 1   123       2       f      s        c


you can use combine_first:

In[44]:df.combine_first(df1).combine_first(df2)[['Window','Label','FeatA','FeatB','FeatC']]Out[44]:    Window  Label FeatA FeatB FeatC0     123      1     h     d     d1     123      2     f     s     c

or you can use merge:

In[30]:df.merge(df1,on=['Window','Label']).merge(df2,on=['Window','Label'])Out[30]:    Window  Label FeatA FeatB FeatC0     123      1     h     d     d1     123      2     f     s     c