Home > database >  Deciles in Python
Deciles in Python

Time:07-14

I want to group a column into deciles and assign points out of 50.

The lowest decile receives 5 points and points are increased in 5 point increments.

With below I am able to group my column into deciles. How do I assign points so the lowest decile has 5 points, 2nd lowest has 10 points so on ..and the highest decile has 50 points.

df = pd.DataFrame({'column'[1,2,2,3,4,4,5,6,6,7,7,8,8,9,10,10,10,12,13,14,16,16,16,18,19,20,20,22,24,28]})


df['decile'] = pd.qcut(df['column'], 10, labels = False)```

CodePudding user response:

Simple enough; you can apply operations between columns directly. Deciles are numbered from 0 through 9, so they are naturally ordered. You want increments of 5 points per decile, so multiplying the deciles by 5 will give you that. Since you want to start at 5, you can offset with a simple sum. The following gives you what I believe you want:

df['points'] = df['decile'] * 5 5

CodePudding user response:

Try this:

df['points'] = df['decile'].add(1).mul(5)

Output:

    column  decile  points
0        1       0       5
1        2       0       5
2        2       0       5
3        3       1      10
4        4       1      10
5        4       1      10
6        5       2      15
7        6       2      15
8        6       2      15
9        7       3      20
10       7       3      20
11       8       3      20
12       8       3      20
13       9       4      25
14      10       4      25
15      10       4      25
16      10       4      25
17      12       5      30
18      13       6      35
19      14       6      35
20      16       6      35
21      16       6      35
22      16       6      35
23      18       7      40
24      19       8      45
25      20       8      45
26      20       8      45
27      22       9      50
28      24       9      50
29      28       9      50

CodePudding user response:

Here's a way that can easily be generalized to different point systems that are not linear with decile:

df['points'] = df.decile.map({d:5 * (d   1) for d in range(10)})

This uses Series.map() to map from each decile value to the desired number of points for that decile using a dictionary.

  • Related