Given a DataFrame and a Dict, how might I get a new column with the weighted sum? The keys in the Dict match the column names in the DataFrame
import pandas as pd
df = pd.DataFrame({'Index': ['aaa', 'bbb', 'ccc'],
'a': [1, 2, 3],
'b': [4, 5, 6],
'c': [7, 8, 9]})
df.set_index('Index', inplace=True)
weights = {'a': 0.5,
'b': 0.2
}
Here I want a pythonic way (I'm currently doing a big loop!) to get the following result:
| Index | a | b | c | weighted_sum |
| aaa | 1 | 4 | 7 | (1 * 0.5) + (4 * 0.2) |
| bbb | 2 | 5 | 8 | (2 * 0.5) + (5 * 0.2) |
| ccc | 3 | 6 | 9 | (3 * 0.5) + (6 * 0.2) |
Note, I won't always know what keys are in the weights dict