将列的平均值设为 100 并按比例变换列中的其他值 (Pandas Python)

Place the mean of the column equal to 100 and transform other values in the column proportionally (Pandas Python)

我有一个这样的 pandas 数据框:

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 中完成?我可以安装任何库。 谢谢!

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

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