我想从以下数据框中为每个type
获取多个自定义的百分位数(0.10, 0.20, ..., 0.50)
值:
type value
0 a 90
1 a 63
2 a 11
3 a 61
4 a 78
5 a 67
6 a 89
7 a 12
8 a 43
9 a 30
10 b 72
11 b 84
12 b 74
13 b 66
14 b 80
15 b 75
16 b 47
17 b 22
18 b 5
19 b 64
20 b 32
21 b 45
我已开始这一步骤:
df['percentile_rank'] = df.groupby('type').value.rank(pct=True).round(2)
输出:
type value percentile_rank
0 a 90 1.00
1 a 63 0.60
2 a 11 0.10
3 a 61 0.50
4 a 78 0.80
5 a 67 0.70
6 a 89 0.90
7 a 12 0.20
8 a 43 0.40
9 a 30 0.30
10 b 72 0.67
11 b 84 1.00
12 b 74 0.75
13 b 66 0.58
14 b 80 0.92
15 b 75 0.83
16 b 47 0.42
17 b 22 0.17
18 b 5 0.08
19 b 64 0.50
20 b 32 0.25
21 b 45 0.33
但我不知道如何得到这样的预期结果:
type top10 top20 top30 top40 top50
0 a 89 78 67 63 61
1 b 80 75 72 66 64
在上面的表格中top10
表示percentile_rank
等于0.90
,top20
表示0.80
,等等。如果没有精确的百分位值,那么我们采用最接近的值,例如top10
表示b
的type
,我使用80
的值,其percentile_rank
是{
提前谢谢你的帮助
更新:
在Andy L.方法的输出中,您可以注意到top55
和top45
的NaN
:
type top95 top90 top85 top80 top75 top70 top65 top60 top55 top50 \
0 e 40.82 41.81 41.82 42.35 43.85 44.42 44.99 45.92 NaN 45.94
top45 top40 top35 top30 top25 top20 top15 top10 top5
0 NaN 46.04 46.25 46.45 46.85 47.49 48.55 49.82 52.18
YOBEN_方法的输出:
type top95 top90 top85 top80 top75 top70 top65 top60 top55 top50 \
0 e 40.704 41.82 41.82 42.326 43.7 44.36 44.94 45.94 45.94 45.94
top45 top40 top35 top30 top25 top20 top15 top10 top5
0 45.94 46.04 46.226 46.42 46.82 47.412 48.412 49.776 52.008
我们可以做
quantile
如果您想使用现有的值,我想您可以使用
pd.cut
和groupby max,如下所示相关问题 更多 >
编程相关推荐