1

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.