pandas-08 pd.cut()的功能和作用

pd.cut()的作用,有点类似给成绩设定优良中差,比如:0-59分为差,60-70分为中,71-80分为优秀等等,在pandas中,也提供了这样一个方法来处理这些事儿。直接上代码:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame np.random.seed(666) score_list = np.random.randint(25, 100, size=20)
print(score_list)
# [27 70 55 87 95 98 55 61 86 76 85 53 39 88 41 71 64 94 38 94] # 指定多个区间
bins = [0, 59, 70, 80, 100] score_cut = pd.cut(score_list, bins)
print(type(score_cut)) # <class 'pandas.core.arrays.categorical.Categorical'>
print(score_cut)
'''
[(0, 59], (59, 70], (0, 59], (80, 100], (80, 100], ..., (70, 80], (59, 70], (80, 100], (0, 59], (80, 100]]
Length: 20
Categories (4, interval[int64]): [(0, 59] < (59, 70] < (70, 80] < (80, 100]]
'''
print(pd.value_counts(score_cut)) # 统计每个区间人数
'''
(80, 100] 8
(0, 59] 7
(59, 70] 3
(70, 80] 2
dtype: int64
''' df = DataFrame()
df['score'] = score_list
df['student'] = [pd.util.testing.rands(3) for i in range(len(score_list))]
print(df)
'''
score student
0 27 1ul
1 70 yuK
2 55 WWK
3 87 EU6
4 95 Vqn
5 98 KAf
6 55 QNT
7 61 HaE
8 86 aBo
9 76 MMa
10 85 Ctc
11 53 5BI
12 39 wBp
13 88 WMB
14 41 q5t
15 71 MjZ
16 64 nTc
17 94 Kyx
18 38 Rlh
19 94 2uV
''' # 使用cut方法进行分箱
print(pd.cut(df['score'], bins))
'''
0 (0, 59]
1 (59, 70]
2 (0, 59]
3 (80, 100]
4 (80, 100]
5 (80, 100]
6 (0, 59]
7 (59, 70]
8 (80, 100]
9 (70, 80]
10 (80, 100]
11 (0, 59]
12 (0, 59]
13 (80, 100]
14 (0, 59]
15 (70, 80]
16 (59, 70]
17 (80, 100]
18 (0, 59]
19 (80, 100]
Name: score, dtype: category
Categories (4, interval[int64]): [(0, 59] < (59, 70] < (70, 80] < (80, 100]]
''' df['Categories'] = pd.cut(df['score'], bins)
print(df)
'''
score student Categories
0 27 1ul (0, 59]
1 70 yuK (59, 70]
2 55 WWK (0, 59]
3 87 EU6 (80, 100]
4 95 Vqn (80, 100]
5 98 KAf (80, 100]
6 55 QNT (0, 59]
7 61 HaE (59, 70]
8 86 aBo (80, 100]
9 76 MMa (70, 80]
10 85 Ctc (80, 100]
11 53 5BI (0, 59]
12 39 wBp (0, 59]
13 88 WMB (80, 100]
14 41 q5t (0, 59]
15 71 MjZ (70, 80]
16 64 nTc (59, 70]
17 94 Kyx (80, 100]
18 38 Rlh (0, 59]
19 94 2uV (80, 100]
''' # 但是这样的方法不是很适合阅读,可以使用cut方法中的label参数
# 为每个区间指定一个label
df['Categories'] = pd.cut(df['score'], bins, labels=['low', 'middle', 'good', 'perfect'])
print(df)
'''
score student Categories
0 27 1ul low
1 70 yuK middle
2 55 WWK low
3 87 EU6 perfect
4 95 Vqn perfect
5 98 KAf perfect
6 55 QNT low
7 61 HaE middle
8 86 aBo perfect
9 76 MMa good
10 85 Ctc perfect
11 53 5BI low
12 39 wBp low
13 88 WMB perfect
14 41 q5t low
15 71 MjZ good
16 64 nTc middle
17 94 Kyx perfect
18 38 Rlh low
19 94 2uV perfect
'''

pandas-08 pd.cut()的功能和作用的更多相关文章

  1. pandas模块实现小爬虫功能-转载

    pandas模块实现小爬虫功能 安装 pip3 install pandas 爬虫代码 import pandas as pd df = pd.read_html("http://www.a ...

  2. pandas,pd.ExcelWriter保存结果到已存在的excel文件中

    背景:pandas支持将DataFrame数据直接保存到excel中   保存的case如下: import pandas as pd with pd.ExcelWriter('a.xls') as ...

  3. pd.qcut, pd.cut, df.groupby()等在分组和聚合方面的应用

    pd.qcut, pd.cut, df.groupby()等在分组和聚合方面的应用 量化交易里, 需要进行大量的分组和统计, 以方便自己处优势的位置/机会. 比如对股价进行趋势分析, 波动性分析, 量 ...

  4. Eventlog Analyzer日志管理系统、日志分析工具、日志服务器的功能及作用

    Eventlog Analyzer日志管理系统.日志分析工具.日志服务器的功能及作用 Eventlog Analyzer是用来分析和审计系统及事件日志的管理软件,能够对全网范围内的主机.服务器.网络设 ...

  5. pandas.DataFrame——pd数据框的简单认识、存csv文件

    接着前天的豆瓣书单信息爬取,这一篇文章看一下利用pandas完成对数据的存储. 回想一下我们当时在最后得到了六个列表:img_urls, titles, ratings, authors, detai ...

  6. Pandas | 08 重建索引

    重新索引会更改DataFrame的行标签和列标签. 可以通过索引来实现多个操作: 重新排序现有数据以匹配一组新的标签. 在没有标签数据的标签位置插入缺失值(NA)标记. import pandas a ...

  7. 4G DTU模块的功能和作用是什么

    4G DTU模块我们可以简单将它理解为使用4G无线通信网络来进行远距离无线传送的终端设备.4G DTU模块基于4G方式进行远距离的数据传输,是专门用于将串口数据转换为IP数据或将IP数据转换为串口数据 ...

  8. css clip样式 属性功能及作用

    clip clip 在学前端的小伙伴前,估计是很少用到的,代码中也是很少看见的,但是,样式中有这样的代码,下面让我们来讲讲他吧! 这个我也做了很久的开发没碰到过这个属性,知道我在一个项目中,有一个功能 ...

  9. Java中this的功能与作用

    粗粒度上来说,Java中关键字this主要有2个功能: 1.表示“当前对象”的引用 (1)作为参数传入 [程序实例1] public class MyObject { public Integer v ...

随机推荐

  1. Linux 设置代理

    一.为系统设置代理编辑文件/etc/profile,增加如下两行 export http_proxy=http://proxy.com:8080/export https_proxy=http://p ...

  2. Research Guide: Pruning Techniques for Neural Networks

    Research Guide: Pruning Techniques for Neural Networks 2019-11-15 20:16:54 Original: https://heartbe ...

  3. Mysql模糊查询like提速优化

    LOCATE('substr',str,pos)方法 SELECT LOCATE('xbar',`foobar`); ###返回0 SELECT LOCATE('bar',`foobarbar`); ...

  4. HandlerMethodReturnValueHandler SpringMVC 参数解析 继承关系以及各解析器解析类型

    I HandlerMethodReturnValueHandler (org.springframework.web.method.support) AbstractMessageConverterM ...

  5. Redis 服务监控 redis-faina 命令查看 和 redis-live 界面查看

    Redis现在在很多公司中应用已经很广泛了,但是如何监控redis,实时的观察redis的性能,下面就关于Redis的常用监控介绍一下. 一.使用redis自带的info命令和monitor命令现在常 ...

  6. jzy3D从入门到弃坑_2使用jzy3D0.9画2D散点图

    jzy3D从入门到弃坑_2 使用jzy3D0.9画2D散点图 觉得有用的话,欢迎一起讨论相互学习~Follow Me 在上一节中安装jzy3D 0.9版本并且运行了3D的例子 https://blog ...

  7. springBoot项目不重新上传jar包,增量升级步骤

    1.把源jar包cp到一个空文件夹里,然后把该jar包解压 jar -xf www.itxm.cn-0.0.1-SNAPSHOT.jar 2.进入BOOT-INF文件夹,找到class文件夹,把要升级 ...

  8. shell基础知识4--别名、采集终端信息

    别名就是一种便捷方式,可以为用户省去输入一长串命令序列的麻烦.下面我们会看到如何 使用 alias 命令创建别名. 直接使用alias就是显示当前有哪些别名,否则就是创建别名 [root@dns-no ...

  9. pytorch使用DataParallel并行化负载不均衡问题

    使用DataParallel进行并行化时的结构如下: 在上图第一行第四个步骤中,GPU-1 其实汇集了所有 GPU 的运算结果.这个对于多分类问题还好,但如果是自然语言处理模型就会出现问题,导致 GP ...

  10. 关于C#编写x86与x64程序的分析

    电脑硬件CPU可以分为x86与x64, x86的机器只能安装32位的操作系统,如XP, WIN7_86, x64的机器既可以安装32位的系统,又可以安装64位的系统,只是在x64的机器上安装32位的系 ...