Search code examples
pythonpandasscale

Scaling pandas column to be between specified min and max numbers


Let us say, I have the following data frame.

Frequency  
20
14
10
8
6
2
1

I want to scale Frequency value from 0 to 1.

Is there a way to do this in Python? I have found something similar here But it doesn't serve my purpose.


Solution

  • Just change a, b = 10, 50 to a, b = 0, 1 in linked answer for upper and lower values for scale:

    a, b = 0, 1
    x, y = df.Frequency.min(), df.Frequency.max()
    df['normal'] = (df.Frequency - x) / (y - x) * (b - a) + a
    print (df)
       Frequency    normal
    0         20  1.000000
    1         14  0.684211
    2         10  0.473684
    3          8  0.368421
    4          6  0.263158
    5          2  0.052632
    6          1  0.000000