Home > Enterprise >  Scale Data per column pd
Scale Data per column pd

Time:09-30

I have a data set that had values entered incorrectly such that the scale needed is 0-4 but it was entered as 0-5, what is the best way to scale down data such as the following table:

a b c
0 2 5
5 4 3
1 4 2
3 4 0

CodePudding user response:

If you want to scale all values in your df, you can use

df = df*4/5

     a    b    c
0  0.0  1.6  4.0
1  4.0  3.2  2.4
2  0.8  3.2  1.6
3  2.4  3.2  0.0

recreating your input:

df = pd.DataFrame({'a': {0: 0, 1: 5, 2: 1, 3: 3}, 'b': {0: 2, 1: 4, 2: 4, 3: 4}, 'c': {0: 5, 1: 3, 2: 2, 3: 0}})

CodePudding user response:

without assuming the the maximum value is 5, you can extract the maximum value and apply the scale down based on it:

df = df / df.max().max() * 4
print(df)

input

 a  b   c
 2  5   5
 8  4   3
 1  4   2
 3  4   0

output

   a    b    c
  1.0  2.5  2.5
  4.0  2.0  1.5
  0.5  2.0  1.0
  1.5  2.0  0.0
  • Related