0

I am fighting with the best optimal (w/o looping etc.) solution for complete the dataframe on the basis of the second one.

For example I have primary df:

      owner  toy   id_toy
0     Simon  Car   11
1     Tommy  Lego  12
2     Kate   Lego  7
3     Kate   Duck  7
4     Kate   Car   11

and second df:

      toy  id_toy  weight  color
0     Car  11      12.00   red
1     Lego 12      5.00    white
2     Duck 7       8.00    yellow

And I would like to fill primary df based on the second df, it shall present like below:

      owner  toy   id_toy  weight  color
0     Simon  Car   11      12.00   red
1     Tommy  Lego  12      5.00    white
2     Kate   Lego  7       5.00    white
3     Kate   Duck  7       8.00    yellow
4     Kate   Car   11      12.00   red

Is it possible to do it in a "few lines" using some pandas functions?

1
  • 3
    pd.merge(df1,df2.drop('toy',1),on='id_toy',how='left') Commented Oct 1, 2020 at 16:10

1 Answer 1

0

Use merge() function as :

df1.merge(df2,how='inner',by='id_toy')
Sign up to request clarification or add additional context in comments.

1 Comment

since there is an ID column you should use that as a source for any sort of merging.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.