merge multiple Pandas dataframe

Question:

I have three dataframes that look like the following:

df1                 df2                     df3
name      id        name      colour        name      type
apple     1         apple     red           apple     fruit
banana    2         banana    yellow        banana    fruit
cucumber  3         cucumber  green         cucumber  vegetable

I would like to merge the dataframes so they look like this:

name      id  colour  type
apple     1   red     fruit
banana    2   yellow  fruit
cucumber  3   green   vegetable

I have tried using merge and concatenate but the output was not how I wanted it or had a bunch of NaN entries. Does anyone have an idea on how to effectively merge the dataframes?

Asked By: luthien aerendell

||

Answers:

try this:

tmp = df1.merge(df2, on='name')
df3.merge(tmp, on='name')
Answered By: bpfrd

For multiple merge you can use functools.reduce.

from functools import reduce
df = reduce(lambda x,y : x.merge(y, on='name'), [df1, df2, df3])
# this work like this : df1.merge(df2, on='name').merge(df3, on='name')
print(df)
# name      id  colour  type
# apple     1   red     fruit
# banana    2   yellow  fruit
# cucumber  3   green   vegetable
Answered By: I'mahdi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.