0

I have a dictionary and a dataframe:

dic = {"A":1,"B":2,"C":3,"D":4}

   key
0   A
1   C
2   D
3   B
4   A
5   C
6   C

How can I populate the dataframe, using the dictionary, to generate a new dataframe as follows:

   key value
0   A   1
1   C   3
2   D   4
3   B   2
4   A   1
5   C   3
6   C   3

Thought about maybe using apply(lambda) function, but with no success.

Thank you!

1 Answers1

0

You could map using dictionary but the simplest way if generating categories would be

 df['Value2'] = pd.factorize(df['key'])[0] + 1
wwnde
  • 26,119
  • 6
  • 18
  • 32