我想将字典中的权重映射到数据框中的某些列并计算索引(实际上是加权平均值)。
import pandas as pd
people = pd.DataFrame(
{
'name': ['Robert', 'Sam', 'Tom'],
'height': [160, 170, 180],
'IQ': [100, 140, 120],
'income': [12, 7, 4]
}
)
weights = {
'height': 0.1,
'IQ': 0.6,
'income': 0.3
}
sudocode 中所需的索引是:
people['index'] =
mean(
people['height'] * weight of height which is 0.1 +
people['IQ'] * weight of IQ which is 0.6 +
people['income'] * weight of income which is 0.3
)
我尝试使用 map
但我必须将数据帧重新格式化为长格式并将字典映射到单列的值,为了完成这项任务,我不能。我不知道如何将它映射到列名
答案 0 :(得分:0)
您可以使用 lambda 函数:
people['index'] = people.apply(lambda row : (row['height']*weights['height'] +
row['IQ']*weights['IQ'] +
row['income']*weights['income'])/3,
axis = 1)
结果:
姓名 | 高度 | 智商 | 收入 | 索引 | |
---|---|---|---|---|---|
0 | 罗伯特 | 160 | 100 | 12 | 26.533333 |
1 | 山姆 | 170 | 140 | 7 | 34.366667 |
2 | 汤姆 | 180 | 120 | 4 | 30.400000 |