Scale values of a particular column of python dataframe between 1-10

Viewed 1902

I have a dataframe which contains youtube videos views, I want to scale these values in the range of 1-10. Below is the sample of how values look like? How do i normalize it in the range of 1-10 or is there any more efficient way to do this thing?

rating
4394029
274358
473691
282858
703750
255967
3298456
136643
796896
2932
220661
48688
4661584
2526119
332176
7189818
322896
188162
157437
1153128
788310
1307902
1 Answers

One possibility is performing a scaling with max.

1 + df / df.max() * 9

       rating
0    6.500315
1    1.343433
2    1.592952
3    1.354073
4    1.880933
5    1.320412
6    5.128909
7    1.171046
8    1.997531
9    1.003670
10   1.276217
11   1.060946
12   6.835232
13   4.162121
14   1.415808
15  10.000000
16   1.404192
17   1.235536
18   1.197075
19   2.443451
20   1.986783
21   2.637193

Similar solution by Wen (now deleted):

1 + (df - df.min()) * 9 / (df.max() - df.min())

       rating
0    6.498887
1    1.339902
2    1.589522
3    1.350546
4    1.877621
5    1.316871
6    5.126922
7    1.167444
8    1.994266
9    1.000000
10   1.272658
11   1.057299
12   6.833941
13   4.159739
14   1.412306
15  10.000000
16   1.400685
17   1.231960
18   1.193484
19   2.440368
20   1.983514
21   2.634189
Related