0

I have two dataframes of the shape: (4000,3) (2000,3) , with the bellow info and cols:

df1:

imo speed length
1 1 4
1 2 4
2 10 10
2 12 10

df2:

imo dwt name
1 52 test1
2 62 test2
3 785 test3
4 353 test4

i would like to add column dwt of df2 to df1 based on the same imo.

imo speed length dwt
1 1 4 52
1 2 4 52
2 10 10 62
2 12 10 62

but when i am trying to do pd.merge(df1,df2, on = 'imo', how = 'inner') , the result is much more rows than the original shape of df1 how is that possible?

JHk1821
  • 146
  • 1
  • 6
  • 20

2 Answers2

1

You can use an alternate way, without merge function:

dwt_lst = []
for imo in df1.imo.values:
   dwt = df2[df2.imo == imo].dwt.values[0]
   dwt_lst.append(dwt)
df1['dwt'] = dwt_lst
JohnV
  • 96
  • 5
0

Just use merge in this way:

pd.merge(df1, df2, how='left', on='imo').drop(columns='name')

Example:

>>> df1 = pd.DataFrame({'imo': [1,1,2,2], 'speed': [1,2,10,12], 'length': [4,4,10,10]})
>>> df2 = pd.DataFrame({'imo': [1,2,3,4], 'dwt': [52,62,785,353], 'name': ['test1','test2','test3','test4']})
>>> pd.merge(df1, df2, how='left', on='imo').drop(columns='name')
   imo  speed  length  dwt
0    1      1       4   52
1    1      2       4   52
2    2     10      10   62
3    2     12      10   62
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50