Home > Blockchain >  merge multiple Pandas dataframe
merge multiple Pandas dataframe

Time:07-21

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?

CodePudding user response:

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

CodePudding user response:

try this:

tmp = df1.merge(df2, on='name')
df3.merge(tmp, on='name')
  • Related