1 回答
TA贡献1798条经验 获得超7个赞
您的错误在于转换为字典。你做到了,
df2.set_index('MSA Code').to_dict()
# {
# "Average Wage": {
# "10180": 71128.571429,
# "10420": 76620.0,
# "11260": 94490.0,
# "13820": 87338.396624,
# "21820": 72080.0
# }
# }
这导致了一个dict-of-dicts。你应该做的是,
df2.set_index('MSA Code')['Average Wage'].to_dict()
# {11260: 94490.0, 21820: 72080.0, 10180: 71128.571429, 13820: 87338.396624, 10420: 76620.0}
要么,
df2.set_index('MSA Code')['Average Wage']
MSA Code
11260 94490.000000
21820 72080.000000
10180 71128.571429
13820 87338.396624
10420 76620.000000
Name: Average Wage, dtype: float64
两者都导致了可以map理解的映射格式。现在您的map调用会产生预期的输出:
df['Wages'] = df['MSA Code'].map(df2.set_index('MSA Code')['Average Wage'])
df
MSA Code Line RPP Wages
0 10180 1.0 91.2 71128.571429
1 10180 2.0 97.4 71128.571429
2 10180 3.0 78.7 71128.571429
3 10180 4.0 93.5 71128.571429
4 10420 1.0 90.4 76620.000000
添加回答
举报