How to create a column based on another dataframe?

Asked

Viewed 39 times

1

I would like to know how to create a column on a dataframe based on the code of another Python dataframe. For example, I have:
df A:

letra
c
b
d
c
a

df B:

letra codigo
   a       10
   b       20
   c       30
   d       40

Expected result is df A to look like this:

letra codigo
   c    30
   b    20
   d    40
   c    30
   a    10
  • I didn’t understand why the question was defined as out of scope, but anyway, test using map. df['codigo'] = df['letra'].map(df2.set_index('letra')['codigo'])

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.