I want to create a new column col2
and calculate using a Dictionary. When a key is not found, I want to calculate the value as 0:
import pandas as pd
df = pd.DataFrame({'col1': {0: 'a', 1: 'b', 2: 'c'}})
d = {'a':1,'b':2}
col1
0 a
1 b
2 c
So I try:
df['col2'] = d.get(df['col1'], 0)
builtins.TypeError: 'Series' objects are mutable, thus they cannot be hashed
Im guessing I need to use map, like this:
df['col2'] = df['col1'].map(d)
But how to get 0 when a key is missing, and not Nan?