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?

onieks
  • 51
  • 1
  • 5

1 Answers1

0

Use merge() function as :

df1.merge(df2,how='inner',by='id_toy')
Surya Lohia.
  • 446
  • 4
  • 16