【问题标题】:Place the mean of the column equal to 100 and transform other values in the column proportionally (Pandas Python)将列的平均值设为 100,并按比例转换列中的其他值(Pandas Python)
【发布时间】:2020-06-10 08:42:00
【问题描述】:

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

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 中做到这一点?我可以安装任何库。 谢谢!

【问题讨论】:

    标签: python python-3.x pandas standardized


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2020-03-11
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 2018-06-23
      • 2014-03-06
      • 1970-01-01
      相关资源
      最近更新 更多