[英]Deciles in Python
I want to group a column into deciles and assign points out of 50.我想将一列分组为十分位数并分配 50 分。
The lowest decile receives 5 points and points are increased in 5 point increments.最低的十分位获得 5 分,分数以 5 分的增量增加。
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.
我如何分配分数,所以最低的十分位数有 5 分,第二低的有 10 分,依此类推..最高的十分位数有 50 分。
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)```
Simple enough;很简单; you can apply operations between columns directly.
您可以直接在列之间应用操作。 Deciles are numbered from 0 through 9, so they are naturally ordered.
十分位数从 0 到 9 编号,因此它们是自然排序的。 You want increments of 5 points per decile, so multiplying the deciles by 5 will give you that.
您希望每十分位数增加 5 分,因此将十分位数乘以 5 即可。 Since you want to start at 5, you can offset with a simple sum.
因为你想从 5 开始,你可以用一个简单的总和来抵消。 The following gives you what I believe you want:
以下内容为您提供了我认为您想要的内容:
df['points'] = df['decile'] * 5 + 5
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
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.这使用
Series.map()
使用字典从每个十分位值映射到该十分位所需的点数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.