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的更多相关文章

  1. Pandas的Categorical Data类型

    pandas从0.15版开始提供分类数据类型,用于表示统计学里有限且唯一性数据集,例如描述个人信息的性别一般就男和女两个数据常用'm'和'f'来描述,有时也能对应编码映射为0和1.血型A.B.O和AB ...

  2. Categorical Data

    This is an introduction to pandas categorical data type, including a short comparison with R's facto ...

  3. pandas的Categorical方法

    对于数据样本的标签,如果我们事先不知道这个样本有多少类别,那么可以对数据集的类别列进行统计,这时我们用pandas的Categorical方法就非常快的实现. 1.说明: 你的数据最好是一个serie ...

  4. 【跟着stackoverflow学Pandas】“Large data” work flows using pandas-pandas大数据处理流程

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  5. [论文]A Link-Based Cluster Ensemble Approach for Categorical Data Clustering

    http://www.cnblogs.com/Azhu/p/4137131.html 这篇论文建议先看了上面这一遍,两篇作者是一样的,方法也一样,这一片论文与上面的不同点在于,使用的数据集是目录数据, ...

  6. 吴裕雄--天生自然python学习笔记:pandas模块读取 Data Frame 数据

    读取行数据 读取一个列数据的语法为: 例如,读取所有学生自然科目的成绩 : import pandas as pd datas = [[65,92,78,83,70], [90,72,76,93,56 ...

  7. Pandas Python For Data Science

  8. Pandas分类

    Pandas分类 categorical data是指分类数据:数据类型为:男女.班级(一班.二班).省份(河北.江苏等),若使用赋值法给变量赋值,例如(男=1,女=0),数字1,0之间没有大小之分, ...

  9. pandas入门10分钟——serries其实就是data frame的一列数据

    10 Minutes to pandas This is a short introduction to pandas, geared mainly for new users. You can se ...

随机推荐

  1. FreeRTOS 任务创建和删除(动态)

    TaskHandle_t taskhandle; TaskHandle_t taskhandle1; void vTask(void *t) { int i = 0; while(1) { i++; ...

  2. SPI简述

    SPI是器件的比较常用的通信协议. SPI总共有四根线: SS:片选线,每个设备都和主机MCU有一条单独片选线相连,片选线拉低意味主机输出,也就是说一个主机可以和多个从机相连,只需要有足够多的片选线. ...

  3. HttpContext & HttpRuntime

    问题引出 HttpContext.Current.Cache .VS. HttpRuntime.Cache HttpRuntime.Cache:获取当前应用程序的Cache HttpContext.C ...

  4. (转)大厂常问到的14个Java面试题

    1. synchronized和reentrantlock异同 相同点 都实现了多线程同步和内存可见性语义 都是可重入锁 不同点 实现机制不同 synchronized通过java对象头锁标记和Mon ...

  5. 使用DateTimeFormatter替换线程不安全的SimpleDateFormat

    原文:https://blog.csdn.net/baofeidyz/article/details/81307478 如何让SimpleDateFormat保持安全运行? 方案一 每次都去new这种 ...

  6. MySQL服务器2

    1.sql的基本语法 对数据库 create database db1; 创建数据库 对表: create database t1(id int,name char(10)); 创建表 show cr ...

  7. EXCEL导入数据到SQL SERVER 2008

    项目中需要导入excel到SQL SERVER数据库 总是报截断, 本质问题是,SQL SERVER导入程序是根据EXCEL的第一行记录 (非标题行)来决定数据长度的 碰到这个问题,可以伪造第一行,然 ...

  8. 利其器:无法在 ".vscode" 文件夹()内创建 "launch.json" 文件。

    无法在 ".vscode" 文件夹()内创建 "launch.json" 文件. https://www.cnblogs.com/lidabo/p/588899 ...

  9. burp插件debug

    java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar burpsuite_community_v2. ...

  10. 2019牛客暑期多校训练营(第十场)Han Xin and His Troops——扩展中国剩余定理

    题意 求解 $n$ 个模方程 $x \equiv a (mod \ b)$,不保证模数互素($1 \leq n \leq 100$,$0 \leq b < a< 10^5$). 分析 套扩 ...