0

Good day all of you,

I have a dataframe similiar to this:

id    |    name    |    personnelNumber
1     |    abcd    |    00013
2     |    efgh    |    00142
19    |    abcd    |    00013
84    |    abcd    |    00013
103   |    efgh    |    00142
117   |    mnop    |    00087

I also have dictionary with random values and the 'personnelNumber' like in my dataframe:

{'00013': 41, '00087': 725, '00142': 19}

My question: Is there a way to add the values into a new column, based on a matching 'personnelNumber'? I'd like to receice something like this:

id    |    name    |    personnelNumber    |    value
1     |    abcd    |    00013              |    41
2     |    efgh    |    00142              |    19
19    |    abcd    |    00013              |    41
84    |    abcd    |    00013              |    41
103   |    efgh    |    00142              |    19
117   |    mnop    |    00087              |    725

Thanks for all advices and help!

finethen
  • 385
  • 1
  • 4
  • 19

1 Answers1

1

Assuming personnelNumber is of type string (because of the leading zeros), you can use pd.Series.map

d = {'00013': 41, '00087': 725, '00142': 19}

df['value'] = df.personnelNumber.map(d)
df

Out:

    id  name personnelNumber  value
0    1  abcd           00013     41
1    2  efgh           00142     19
2   19  abcd           00013     41
3   84  abcd           00013     41
4  103  efgh           00142     19
5  117  mnop           00087    725
Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32