1

I have a dataframe like this

   c1 
0   a
1   b
2   c
3   d
4   b

and
a dictionary like this:

di = {
'a': 10, 'b': 20, 'c': 30, 'd': 40
}

I want column c2 in my DataFrame like this

   c1  c2
0   a  10
1   b  20
2   c  30
3   d  40
4   b  20

Here is the code to generate your df and di:

df = pd.DataFrame({'c1':['a', 'b', 'c', 'd', 'b']})
di = {
'a': 10, 'b': 20, 'c': 30, 'd': 40
}
Ric S
  • 9,073
  • 3
  • 25
  • 51
luckyCasualGuy
  • 641
  • 1
  • 5
  • 15

1 Answers1

2

You can use the map module of a pandas.Series object

df['c2'] = df['c1'].map(di)

Output

#   c1  c2
# 0  a  10
# 1  b  20
# 2  c  30
# 3  d  40
# 4  b  20
Ric S
  • 9,073
  • 3
  • 25
  • 51