0

I have a dictionary like this:

mydic = {'mara': 1, 'juli':2}

I have dataframe like this:

data = [['mara', 10], ['sara', 15], ['juli', 14]]
df = pd.DataFrame(data, columns=['Name', 'Age'])

I want to be able to do something like this:

df['id'] = df['Name'].apply(mydic)

so the rsult would look like this:

Name   Age  id
mara   10    1
juli   14    2
sara   15    null

I know the code I have written above does not work since I have not passed any key to the dictionary but not sure how can I accomplish that.

sariii
  • 2,020
  • 6
  • 29
  • 57

1 Answers1

3

Use map

df=df.assign(ids=df.Name.map(mydic))
wwnde
  • 26,119
  • 6
  • 18
  • 32