I have a pandas dataframe:
df = pd.DataFrame({'col1': ['3 a, 3 ab, 1 b',
'4 a, 4 ab, 1 b, 1 d',
np.nan] })
and a dictionary
di = {'a': 10.0,
'ab': 2.0,
'b': 1.5,
'd': 1.0,
np.nan: 0.0}
Using values from the dictionary, I want to evaluate the dataframe rows like this:
3*10.0 + 3*2.0 + 1*1.5 giving me a final output that looks like this:
pd.DataFrame({'col1': ['3 a, 3 ab, 1 b',
'4 a, 4 ab, 1 b, 1 d',
'np.nan'], 'result': [37.5,
50.5,
0] })
So, far I could only replace ',' by '+'
df['col1'].str.replace(',',' +').str.split(' ')