我有一个数据框如下:
Col1 Price
1 Plastic 50
2 Metal 100
3 Plastic 40
我想用 Price 中的值替换 Col1 中的值,所以我得到:
Col1 Price
1 45 50
2 100 100
3 45 40
我已经这样做了:
df.groupby('Col1').mean()['Price']
但我不知道如何替换这些值,也许使用 map ?
你是对的-map
可以用来这样做:
df['Col1'] = df['Col1'].map(df.groupby('Col1')['Price'].mean())
df
Col1 Price
1 45 50
2 100 100
3 45 40
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)