首页 > 解决方案 > 将列的平均值设为 100,并按比例转换列中的其他值(Pandas Python)

问题描述

我有一个像这样的熊猫数据框:

City  Variable1 
c1    1234      
c2    2222      
c3    1111      
c4    2224      

我想应用一种标准化形式:

例如,列 Variable1 的平均值为 1697.75。如果我将此均值设为 100 并进行比例计算,我将得到所需的输出:

City | Variable1  
_________________
c1   | 72,68     
c2   | 130,88    
c3   | 65,44     
c4   | 131      

我怎样才能在 Python 中做到这一点?我可以安装任何库。谢谢!

标签: pythonpython-3.xpandasstandardized

解决方案


您可以计算列的平均值并简单地将每一行除以:

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

推荐阅读