0

I am wondering how I can most efficiently do the following operation so that I can also upscale it to dataframes with million rows+. I have 2 panda dataframes:

Data1:

Position    Letter
1           a
2           b
3           c
4           b
5           a

Data2:

Weight    Letter
1           a
2           b
3           c

Now I want to create an extra column(weight) in data 1 resulting in the following:

Position    Letter   Weight
1           a          1
2           b          2
3           c          3
4           b          2
5           a          1
HME
  • 103
  • 8

1 Answers1

1

Best way is to use merge:

df = df1.merge(df2, on=['Letter'])
print(df)

   Position Letter  Weight
0         1      a       1
1         5      a       1
2         2      b       2
3         4      b       2
4         3      c       3
NYC Coder
  • 7,424
  • 2
  • 11
  • 24