python利用pd.cut()和pd.qcut()对数据进行分箱操作
作者:cbright63 时间:2022-03-26 07:57:45
1.cut()可以实现类似于对成绩进行优良统计的功能,来看代码示例。
假如我们有一组学生成绩,我们需要将这些成绩分为不及格(0-59)、及格(60-70)、良(71-85)、优(86-100)这几组。这时候可以用到cut()
import numpy as np
import pandas as pd
# 我们先给 scores传入30个从0到100随机的数
scores = np.random.uniform(0,100,size=30)
# 然后使用 np.round()函数控制数据精度
scores = np.round(scores,1)
# 指定分箱的区间
grades = [0,59,70,85,100]
cuts = pd.cut(scores,grades)
print('\nscores:')
print(scores)
print('\ncuts:')
print(cuts)
# 我们还可以计算出每个箱子中有多少个数据
print('\ncats.value_counts:')
print(pd.value_counts(cuts))
======output:======
scores:
[ 6. 50.8 80.2 22.1 60.1 75.1 30.8 50.8 81.6 17.4 13.4 24.3 67.3 84.4
63.4 21.3 17.2 3.7 40.1 12.4 15.7 23.1 67.4 94.8 72.6 12.8 81. 82.
70.2 54.1]
cuts:
[(0, 59], (0, 59], (70, 85], (0, 59], (59, 70], ..., (0, 59], (70, 85], (70, 85], (70, 85], (0, 59]]
Length: 30
Categories (4, interval[int64]): [(0, 59] < (59, 70] < (70, 85] < (85, 100]]
cuts.value_counts:
(0, 59] 17
(70, 85] 8
(59, 70] 4
(85, 100] 1
dtype: int64
默认情况下,cat()的区间划分是左开右闭,可以传递right=False来改变哪一边是封闭的
代码示例:
cuts = pd.cut(scores,grades,right=False)
也可以通过向labels选项传递一个列表或数组来传入自定义的箱名
代码示例:
group_names = ['不及格','及格','良','优秀']
cuts = pd.cut(scores,grades,labels=group_names)
当我们不需要自定义划分区间时,而是需要根据数据中最大值和最小值计算出等长的箱子。
代码示例:
# 将成绩均匀的分在四个箱子中,precision=2的选项将精度控制在两位
cuts = pd.cut(scores,4,precision=2)
2.qcut()可以生成指定的箱子数,然后使每个箱子都具有相同数量的数据
代码示例:
import numpy as np
import pandas as pd
# 正态分布
data = np.random.randn(100)
# 分四个箱子
cuts = pd.qcut(data,4)
print('\ncuts:')
print(cuts)
print('\ncuts.value_counts:')
print(pd.value_counts(cuts))
======output:======
cuts:
[(-0.745, -0.0723], (0.889, 2.834], (-0.745, -0.0723], (0.889, 2.834], (0.889, 2.834], ..., (-0.745, -0.0723], (-0.0723, 0.889], (-3.1599999999999997, -0.745], (-0.745, -0.0723], (-0.0723, 0.889]]
Length: 100
Categories (4, interval[float64]): [(-3.1599999999999997, -0.745] < (-0.745, -0.0723] < (-0.0723, 0.889] <
(0.889, 2.834]]
cuts.value_counts:
(0.889, 2.834] 25
(-0.0723, 0.889] 25
(-0.745, -0.0723] 25
(-3.1599999999999997, -0.745] 25
dtype: int64
来源:https://blog.csdn.net/marioivy/article/details/96766913
标签:python,pd.cut(),pd.qcut()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用php-timeit估计php函数的执行时间
2023-10-07 19:56:50
Python数学建模PuLP库线性规划实际案例编程详解
2021-04-29 19:12:56
Python制作进度条的四种方法总结
2022-05-30 05:21:00
![](https://img.aspxhome.com/file/2023/1/78561_0s.gif)
python实现linux下使用xcopy的方法
2022-08-16 07:52:09
python3 循环读取excel文件并写入json操作
2022-11-07 15:40:11
![](https://img.aspxhome.com/file/2023/3/82673_0s.jpg)
python实战教程之自动扫雷
2022-08-16 01:05:48
![](https://img.aspxhome.com/file/2023/5/84975_0s.jpg)
Python量化交易实战之使用Resample函数转换“日K”数据
2023-07-30 07:31:11
![](https://img.aspxhome.com/file/2023/2/105232_0s.png)
python爬虫爬取笔趣网小说网站过程图解
2022-10-06 10:56:50
![](https://img.aspxhome.com/file/2023/7/79587_0s.png)
人工智能学习Pytorch张量数据类型示例详解
2021-09-13 01:33:08
![](https://img.aspxhome.com/file/2023/5/72795_0s.png)
Python入门_浅谈逻辑判断与运算符
2023-11-09 05:45:26
![](https://img.aspxhome.com/file/2023/8/64668_0s.png)
BluePrint CSS框架0.9版发布
2009-06-03 21:02:00
Python3逻辑运算符与成员运算符
2021-03-29 18:59:14
基于Python实现MUI侧滑菜单a标签跳转
2021-06-12 22:49:22
![](https://img.aspxhome.com/file/2023/1/70851_0s.jpg)
python信号量,条件变量和事件详解
2021-10-06 18:14:23
![](https://img.aspxhome.com/file/2023/4/109884_0s.png)
Python读取表格类型文件代码实例
2023-08-24 22:04:16
PHP中的Trait 特性及作用
2023-11-21 08:04:15
Python标准库之Math,Random模块使用详解
2021-02-09 22:33:13
python通过re正则表达式切割中英文的操作
2021-11-29 04:41:23
Python hashlib常见摘要算法详解
2023-07-29 20:04:09
利用JSP的思想来做ASP
2005-09-09 10:10:00