Placing every value in its percentile in Pandas(将每个值放在 Pandas 的百分位数中)
本文介绍了将每个值放在 Pandas 的百分位数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑具有以下百分位数的系列:
Consider a Series with the following percentiles:
> df['col_1'].describe(percentiles=np.linspace(0, 1, 20))
count 13859.000000
mean 421.772842
std 14665.298998
min 1.201755
0% 1.201755
5.3% 1.430695
10.5% 1.438417
15.8% 1.466462
21.1% 1.473050
26.3% 1.500834
31.6% 1.512218
36.8% 1.542935
42.1% 1.579845
47.4% 1.647162
50% 1.690612
52.6% 1.749047
57.9% 1.955589
63.2% 2.344475
68.4% 3.075641
73.7% 4.466094
78.9% 8.410964
84.2% 14.998738
89.5% 41.363612
94.7% 162.865079
100% 1511013.790233
max 1511013.790233
Name: col_1, dtype: float64
我想获得另一列 col_2,其中包含在上述计算中分配给每一行的百分位数.
I would like to get another column col_2 with the percentile each row was assigned to in the calculation made above.
我怎样才能在 Pandas 中做到这一点?
How can I do that in Pandas?
推荐答案
df2 = pd.DataFrame(range(1000))
df2.columns = ['a1']
df2['percentile'] = pd.qcut(df2.a1,100, labels=False)
或者省略标签以查看范围
Or leave out labels to see the range
请注意,在 Python 3 中,使用 Pandas 0.16.2(截至今天的最新版本),您需要使用 list(range(1000)) 而不是 range(1000) 使上述工作.
Note that in Python 3, with Pandas 0.16.2 (latest version as of today), you need to use list(range(1000)) instead of range(1000) for the above to work.
这篇关于将每个值放在 Pandas 的百分位数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:将每个值放在 Pandas 的百分位数中
基础教程推荐
猜你喜欢
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
