python - 将列的平均值设为 100,并按比例转换列中的其他值(Pandas Python)
问题描述
我有一个像这样的熊猫数据框:
City Variable1
c1 1234
c2 2222
c3 1111
c4 2224
我想应用一种标准化形式:
- 列的平均值等于 100。
- 这些值与平均值成比例地转换。
例如,列 Variable1 的平均值为 1697.75。如果我将此均值设为 100 并进行比例计算,我将得到所需的输出:
City | Variable1
_________________
c1 | 72,68
c2 | 130,88
c3 | 65,44
c4 | 131
我怎样才能在 Python 中做到这一点?我可以安装任何库。谢谢!
解决方案
您可以计算列的平均值并简单地将每一行除以:
df1[" Variable1 "] = df1[" Variable1 "] / df1[' Variable1 '].mean() * 100
输出:
City Variable1
0 c1 72.684435
1 c2 130.879105
2 c3 65.439552
3 c4 130.996908
推荐阅读
- python - pandas:创建一个从 1 到每组长度的列
- c# - 我可以用 IEnumerable 写这个 lambda
而不是 IEnumerable ? - typescript - 使用 webpack 编译打字稿时出错
- c# - 如何为每个对象创建一个数组并将其添加到列表(c#)
- python - 如何在 Python 中同时读取 ANSI 和 Unicode 编码文件
- loops - 使用 lisp 循环宏的高级循环
- sql-server - 通过 Flyway 运行批处理脚本时出现 SQL 错误 586
- python - 使用 Python 和 BOTO3 列出 AWS IP 和设备
- aws-step-functions - 为什么没有错误时阶跃函数会自行取消
- r - 为什么某些 R 代码必须包含在 Bookdown 的 index.Rmd 中?