The Series is as below:
value
aa aa bb cc
dd ee aa
ff aa cc
I want to count the occurrence of a word in the row and multiply it with weight given in the dictionary
weights = {
'aa':1,
'bb':1,
'cc':0.5
}
The resultant should be
value_score
3.5
1
1.5
Above could be explained as sum(occurrence of word in dictionary * weight from dictionary) i.e for first value it is 2*1 + 1*1 + 1*0.5 = 3.5
I have currently implemented using str.count, but as more values come in, it is not efficent
df['value_score'] = (df['value'].str.count('aa', regex=False) * weights['aa'] +
df['value'].str.count('bb', regex=False) * weights['bb'] +
df['value'].str.count('cc', regex=False) * weights['cc'] )