Search code examples
pythonpython-3.xpandasstandardized

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


I have a pandas dataframe like this:

City  Variable1 
c1    1234      
c2    2222      
c3    1111      
c4    2224      

I would like to apply a form of standardization where:

  • the mean of the column is placed equal to 100.
  • the values are transformed proportionally to the mean.

For instance, the mean of the column Variable1 is 1697.75. If I place this mean equal to 100 and I make proportions, I have this desired output:

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

How can i do it in Python? I can install any library. Thank you!


Solution

  • You can compute the mean of the column and simply divide each row by that:

    df1[" Variable1 "] = df1[" Variable1 "] / df1[' Variable1 '].mean() * 100
    

    Output:

       City Variable1
    0   c1  72.684435
    1   c2  130.879105
    2   c3  65.439552
    3   c4  130.996908