Pandas的Categorical Data
http://liao.cpython.org/pandas15/
- Docs »
- Pandas的Categorical Data类型
15. Pandas的Categorical Data
pandas从0.15版开始提供分类数据类型,用于表示统计学里有限且唯一性数据集,例如描述个人信息的性别一般就男和女两个数据常用'm'和'f'来描述,有时也能对应编码映射为0和1。血型A、B、O和AB型等选择可以映射为0、1、2、3这四个数字分别代表各个血型。pandas里直接就有categorical类型,可以有效地对数据进行分组进行相应的汇总统计工作。
当DataFrame的某列(字段)上的数据值是都是某有限个数值的集合里的值的时候,例如:性别就男和女,有限且唯一。这列可以采用Categorical Data类型来存储、统计。
pandas的Categorical Data类型灵感来源于Data wareHorsing数据仓库里的维度表设计理念,即某列数据存储的不是数据本身,而是该数据对应的编码(有称为分类、字典编码) 这些编码比数据本身存储依赖的空间小,但能基于编码统计汇总的速度要比数据本身的存储、统计速度要快。
15.1 如何理解Categorical Data?
下面看一张某水果超市的供货商表(表1):
| 供货商 | 水果 | 价格 |
|---|---|---|
| 1 | apple | 5.20 |
| 2 | pearl | 3.50 |
| 3 | orange | 7.30 |
| 5 | apple | 5.00 |
| 6 | orange | 7.50 |
| 7 | orange | 7.30 |
| 9 | apple | 5.20 |
| 4 | pearl | 3.70 |
| 8 | orange | 7.30 |
第2列是各个水果供应商的能供应的水果类型,目前市场也就apple、pearl、orange三种水果可以买到,对于一个大超市而言可能这个表很长、有很多的水果供应商,假设有1亿条数据,那么数据存储所需空间主要浪费在水果名字上了,其他字段都是数值型的数据,而水果这一列是字符串型的,很占空间,如何能降低这张大表的存储空间浪费呢? 设计一个辅助的水果编码表(表2):
| 编码 | 水果 |
|---|---|
| 0 | apple |
| 1 | pearl |
| 2 | orange |
那么供应商的表就变为(表3):
| 供货商 | 水果 | 价格 |
|---|---|---|
| 1 | 0 | 5.20 |
| 2 | 1 | 3.50 |
| 3 | 2 | 7.30 |
| 5 | 0 | 5.00 |
| 6 | 2 | 7.50 |
| 7 | 2 | 7.30 |
| 9 | 0 | 5.20 |
| 4 | 1 | 3.70 |
| 8 | 2 | 7.30 |
变化后的表的数据存储所需的空间量就下来了。也就是说在供应商表里存储的不是水果名数据本身而是存储的水果对应的编码值(通常用整形数据)。可以查供应商表里水果的编码再查辅助的编码表找到水果名。这个水果的编码表在数据仓库里称为维度表(dimension tables)。 而pandas的categorical data的作用就是构建并依赖这个维度表,即例子里的水果编码表。pandas里维度表里记录着若干且唯一的几个分类,可以通过categorical数据的categories 属性获得而数据的所一一对应的编码可以通过codes获得。
| 编码 | 水果 |
|---|---|
| 0 | apple |
| 1 | pearl |
| 2 | orange |
当DataFrame里的某列数据采用categorical Data方式,那么这列数据的存储会大大降低。
import pandas as pd
import time
idx = [1,2,3,5,6,7,9,4,8]
name = ["apple","pearl","orange", "apple","orange","orange","apple","pearl","orange"]
price = [5.20,3.50,7.30,5.00,7.50,7.30,5.20,3.70,7.30]
df = pd.DataFrame({ "fruit": name , "price" : price}, index = idx)
print df,"\n"
print df.memory_usage(),"\n"
print df.dtypes
print "*" * 20
df['fruit'] = df['fruit'].astype('category')
print df
print df.memory_usage(),"\n"
print df.dtypes
程序的执行结果:
fruit price
1 apple 5.2
2 pearl 3.5
3 orange 7.3
5 apple 5.0
6 orange 7.5
7 orange 7.3
9 apple 5.2
4 pearl 3.7
8 orange 7.3
fruit 72
price 72
dtype: int64
fruit object
price float64
dtype: object
********************
fruit price
1 apple 5.2
2 pearl 3.5
3 orange 7.3
5 apple 5.0
6 orange 7.5
7 orange 7.3
9 apple 5.2
4 pearl 3.7
8 orange 7.3
fruit 33
price 72
dtype: int64
fruit category
price float64
dtype: object
最初创建的DataFrame变量df的各个列的类型:
fruit object
price float64
dtype: object
经语句df['fruit'] = df['fruit'].astype('category')将fruit列由Series改为了category类型。
fruit category
price float64
dtype: object
请注意fruit列的类型的变化。正是因为fruit采用了category类型,其存储所需的空间由之前的
fruit 72
price 72
dtype: int64
变为
fruit 33
price 72
dtype: int64
即72变为33,变化了,尽管原始的DataFrame数据量不大,所以变化比率也不大。读者可以适当加大df的数据长度,可以看到很明显的存储容量的降低。
import pandas as pd
import time
idx = [1,2,3,5,6,7,9,4,8]
name = ["apple","pearl","orange", "apple","orange","orange","apple","pearl","orange"]
price = [5.20,3.50,7.30,5.00,7.50,7.30,5.20,3.70,7.30]
#df = pd.DataFrame({ "fruit": name , "price" : price}, index = idx)
N = 100000
df = pd.DataFrame({ "fruit": name * N, "price" : price * N}, index = idx * N)
print df[:4]
print df.memory_usage(),"\n"
print df.dtypes
print "*" * 20
df['fruit'] = df['fruit'].astype('category')
print df[:4]
print df.memory_usage(),"\n"
print df.dtypes
执行结果:
fruit price
1 apple 5.2
2 pearl 3.5
3 orange 7.3
5 apple 5.0
fruit 7200000
price 7200000
dtype: int64
fruit object
price float64
dtype: object
********************
fruit price
1 apple 5.2
2 pearl 3.5
3 orange 7.3
5 apple 5.0
fruit 900024
price 7200000
dtype: int64
fruit category
price float64
dtype: object
15.2 理解category
总结一下pandas的category数据,两次打印DataFrame数据df的结果都是一样的,但是第二次打印的df是其fruit列经语句df['fruit'] = df['fruit'].astype('category')改变了其数据类型已不是Series而是category类型,该列存储所需的内存使用容量大大降低。
import pandas as pd
import time
idx = [1,2,3,5,6,7,9,4,8]
name = ["apple","pearl","orange", "apple","orange","orange","apple","pearl","orange"]
price = [5.20,3.50,7.30,5.00,7.50,7.30,5.20,3.70,7.30]
#df = pd.DataFrame({ "fruit": name , "price" : price}, index = idx)
N = 1
df = pd.DataFrame({ "fruit": name * N, "price" : price * N}, index = idx * N)
df['fruit'] = df['fruit'].astype('category')
print df,"\n"
print "df.price.values\n", df.price.values,"\n"
print "df.fruit.values\n", df.fruit.values, "\n"
print "df.fruit.values.codes\n",df.fruit.values.codes, "\n"
print "df.fruit.values.categories\n",df.fruit.values.categories, "\n"
fruit列是category类型的,通过codes和categorie组合出fruit的values。
fruit price
1 apple 5.2
2 pearl 3.5
3 orange 7.3
5 apple 5.0
6 orange 7.5
7 orange 7.3
9 apple 5.2
4 pearl 3.7
8 orange 7.3
df.price.values
[5.2 3.5 7.3 5. 7.5 7.3 5.2 3.7 7.3]
df.fruit.values
[apple, pearl, orange, apple, orange, orange, apple, pearl, orange]
Categories (3, object): [apple, orange, pearl]
df.fruit.values.codes
[0 2 1 0 1 1 0 2 1]
df.fruit.values.categories
Index([u'apple', u'orange', u'pearl'], dtype='object')
values对应于表1里的第2列即显示输出时“水果”,codes对应于表3的第2列即存储时“水果”列,categories对应于表2的“水果”列即有限唯一的一个集合。
15.3 总结
Categorical Data数据由codes和categories组成,categories是有限且唯一的分类集合,codes是原数据对应的分类的编码, Categorical Data不要求有限并唯一。
Pandas的Categorical Data的更多相关文章
- Pandas的Categorical Data类型
pandas从0.15版开始提供分类数据类型,用于表示统计学里有限且唯一性数据集,例如描述个人信息的性别一般就男和女两个数据常用'm'和'f'来描述,有时也能对应编码映射为0和1.血型A.B.O和AB ...
- Categorical Data
This is an introduction to pandas categorical data type, including a short comparison with R's facto ...
- pandas的Categorical方法
对于数据样本的标签,如果我们事先不知道这个样本有多少类别,那么可以对数据集的类别列进行统计,这时我们用pandas的Categorical方法就非常快的实现. 1.说明: 你的数据最好是一个serie ...
- 【跟着stackoverflow学Pandas】“Large data” work flows using pandas-pandas大数据处理流程
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- [论文]A Link-Based Cluster Ensemble Approach for Categorical Data Clustering
http://www.cnblogs.com/Azhu/p/4137131.html 这篇论文建议先看了上面这一遍,两篇作者是一样的,方法也一样,这一片论文与上面的不同点在于,使用的数据集是目录数据, ...
- 吴裕雄--天生自然python学习笔记:pandas模块读取 Data Frame 数据
读取行数据 读取一个列数据的语法为: 例如,读取所有学生自然科目的成绩 : import pandas as pd datas = [[65,92,78,83,70], [90,72,76,93,56 ...
- Pandas Python For Data Science
- Pandas分类
Pandas分类 categorical data是指分类数据:数据类型为:男女.班级(一班.二班).省份(河北.江苏等),若使用赋值法给变量赋值,例如(男=1,女=0),数字1,0之间没有大小之分, ...
- pandas入门10分钟——serries其实就是data frame的一列数据
10 Minutes to pandas This is a short introduction to pandas, geared mainly for new users. You can se ...
随机推荐
- OpenStack kilo版(5) Neutron部署
neutron简介: Neutron 通过 plugin 和 agent 提供的网络服务. plugin 位于 Neutron server,包括 core plugin 和 service plug ...
- 阿里P7详细解答JVM性能调优之监控工具
javap 和 javac javac -verbose 类名.java java -verbose 类名 javap -c 类名 javap -verbose 类名 JAVAP -HELP 用法: ...
- Linux学习之四-Linux发行版及版本比较
Linux发行版及版本比较 三大家族: Fedora是基于RHEL,CentOS,Scientific Linux, 和Oracle Linux的社区版本.相比RHEL,Fedora打包了显著的更多的 ...
- (转)大厂常问到的14个Java面试题
1. synchronized和reentrantlock异同 相同点 都实现了多线程同步和内存可见性语义 都是可重入锁 不同点 实现机制不同 synchronized通过java对象头锁标记和Mon ...
- 用 Redis 实现延时任务
原文:https://cloud.tencent.com/developer/article/1358266 1.什么是延时任务 延时任务,顾名思义,就是延迟一段时间后才执行的任务.延时任务的使用还是 ...
- Go语言使用Godep进行包管理
一.为什么要包管理 默认Go的第三方包都是放在Gopath的src目录下,而且这些包都没有版本号的概念,这样的可能会出现一些问题. 举个例子:当A同事将开发完程序之后,此时可能引用了一个第三方的包,过 ...
- visual studio调试exe程序
标题:How to debug and profile any EXE with Visual Studio 文章:https://devblogs.microsoft.com/visualstudi ...
- java相关网址汇总(myself)
jar包下载网址 https://www.mvnjar.com/ 或者 https://mvnrepository.com/ 或者 http://www.java2s.com/Open-Source/ ...
- JS控制SVG缩放+鼠标控制事件
话不多说,直接上代码吧,不行你砍我... <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- javascript常用工具类util.js
//如果大家想要补充,请留言 /** * 判断指定名称的复选框是否被选中 * * @param {} * chname复选框名称 */ function chkCheckCha(chname) { v ...