广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Pandas数据类型之category的用法
  • 348
分享到

Pandas数据类型之category的用法

category的用法Pythonpandas 2022-06-02 22:06:19 348人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

创建cateGory 使用Series创建 在创建Series的同时添加dtype="category"就可以创建好category了。category分为两部分,一部分是order,一部分是字面量: In [1

创建cateGory

使用Series创建

在创建Series的同时添加dtype="category"就可以创建好category了。category分为两部分,一部分是order,一部分是字面量:


In [1]: s = pd.Series(["a", "b", "c", "a"], dtype="category")

In [2]: s
Out[2]: 
0    a
1    b
2    c
3    a
dtype: category
Categories (3, object): ['a', 'b', 'c']

可以将DF中的Series转换为category:


In [3]: df = pd.DataFrame({"A": ["a", "b", "c", "a"]})

In [4]: df["B"] = df["A"].astype("category")

In [5]: df["B"]
Out[32]: 
0    a
1    b
2    c
3    a
Name: B, dtype: category
Categories (3, object): [a, b, c]

可以创建好一个pandas.Categorical ,将其作为参数传递给Series:


In [10]: raw_cat = pd.Categorical(
   ....:     ["a", "b", "c", "a"], categories=["b", "c", "d"], ordered=False
   ....: )
   ....: 

In [11]: s = pd.Series(raw_cat)

In [12]: s
Out[12]: 
0    NaN
1      b
2      c
3    NaN
dtype: category
Categories (3, object): ['b', 'c', 'd']

使用DF创建

创建DataFrame的时候,也可以传入 dtype="category":


In [17]: df = pd.DataFrame({"A": list("abca"), "B": list("bccd")}, dtype="category")

In [18]: df.dtypes
Out[18]: 
A    category
B    category
dtype: object

DF中的A和B都是一个category:


In [19]: df["A"]
Out[19]: 
0    a
1    b
2    c
3    a
Name: A, dtype: category
Categories (3, object): ['a', 'b', 'c']

In [20]: df["B"]
Out[20]: 
0    b
1    c
2    c
3    d
Name: B, dtype: category
Categories (3, object): ['b', 'c', 'd']

或者使用df.astype("category")将DF中所有的Series转换为category:


In [21]: df = pd.DataFrame({"A": list("abca"), "B": list("bccd")})

In [22]: df_cat = df.astype("category")

In [23]: df_cat.dtypes
Out[23]: 
A    category
B    category
dtype: object

创建控制

默认情况下传入dtype='category' 创建出来的category使用的是默认值:

Categories是从数据中推断出来的。

Categories是没有大小顺序的。

可以显示创建CategoricalDtype来修改上面的两个默认值:


In [26]: from pandas.api.types import CategoricalDtype

In [27]: s = pd.Series(["a", "b", "c", "a"])

In [28]: cat_type = CategoricalDtype(categories=["b", "c", "d"], ordered=True)

In [29]: s_cat = s.astype(cat_type)

In [30]: s_cat
Out[30]: 
0    NaN
1      b
2      c
3    NaN
dtype: category
Categories (3, object): ['b' < 'c' < 'd']

同样的CategoricalDtype还可以用在DF中:


In [31]: from pandas.api.types import CategoricalDtype

In [32]: df = pd.DataFrame({"A": list("abca"), "B": list("bccd")})

In [33]: cat_type = CategoricalDtype(categories=list("abcd"), ordered=True)

In [34]: df_cat = df.astype(cat_type)

In [35]: df_cat["A"]
Out[35]: 
0    a
1    b
2    c
3    a
Name: A, dtype: category
Categories (4, object): ['a' < 'b' < 'c' < 'd']

In [36]: df_cat["B"]
Out[36]: 
0    b
1    c
2    c
3    d
Name: B, dtype: category
Categories (4, object): ['a' < 'b' < 'c' < 'd']

转换为原始类型

使用Series.astype(original_dtype) 或者 np.asarray(categorical)可以将Category转换为原始类型:


In [39]: s = pd.Series(["a", "b", "c", "a"])

In [40]: s
Out[40]: 
0    a
1    b
2    c
3    a
dtype: object

In [41]: s2 = s.astype("category")

In [42]: s2
Out[42]: 
0    a
1    b
2    c
3    a
dtype: category
Categories (3, object): ['a', 'b', 'c']

In [43]: s2.astype(str)
Out[43]: 
0    a
1    b
2    c
3    a
dtype: object

In [44]: np.asarray(s2)
Out[44]: array(['a', 'b', 'c', 'a'], dtype=object)

categories的操作

获取category的属性

Categorical数据有 categoriesordered 两个属性。可以通过s.cat.categoriess.cat.ordered来获取:


In [57]: s = pd.Series(["a", "b", "c", "a"], dtype="category")

In [58]: s.cat.categories
Out[58]: Index(['a', 'b', 'c'], dtype='object')

In [59]: s.cat.ordered
Out[59]: False

重排category的顺序:


In [60]: s = pd.Series(pd.Categorical(["a", "b", "c", "a"], categories=["c", "b", "a"]))

In [61]: s.cat.categories
Out[61]: Index(['c', 'b', 'a'], dtype='object')

In [62]: s.cat.ordered
Out[62]: False

重命名categories

通过给s.cat.categories赋值可以重命名categories:


In [67]: s = pd.Series(["a", "b", "c", "a"], dtype="category")

In [68]: s
Out[68]: 
0    a
1    b
2    c
3    a
dtype: category
Categories (3, object): ['a', 'b', 'c']

In [69]: s.cat.categories = ["Group %s" % g for g in s.cat.categories]

In [70]: s
Out[70]: 
0    Group a
1    Group b
2    Group c
3    Group a
dtype: category
Categories (3, object): ['Group a', 'Group b', 'Group c']

使用rename_categories可以达到同样的效果:


In [71]: s = s.cat.rename_categories([1, 2, 3])

In [72]: s
Out[72]: 
0    1
1    2
2    3
3    1
dtype: category
Categories (3, int64): [1, 2, 3]

或者使用字典对象:


# You can also pass a dict-like object to map the renaming
In [73]: s = s.cat.rename_categories({1: "x", 2: "y", 3: "z"})

In [74]: s
Out[74]: 
0    x
1    y
2    z
3    x
dtype: category
Categories (3, object): ['x', 'y', 'z']

使用add_categories添加category

可以使用add_categories来添加category:


In [77]: s = s.cat.add_categories([4])

In [78]: s.cat.categories
Out[78]: Index(['x', 'y', 'z', 4], dtype='object')

In [79]: s
Out[79]: 
0    x
1    y
2    z
3    x
dtype: category
Categories (4, object): ['x', 'y', 'z', 4]

使用remove_categories删除category


In [80]: s = s.cat.remove_categories([4])

In [81]: s
Out[81]: 
0    x
1    y
2    z
3    x
dtype: category
Categories (3, object): ['x', 'y', 'z']

删除未使用的cagtegory


In [82]: s = pd.Series(pd.Categorical(["a", "b", "a"], categories=["a", "b", "c", "d"]))

In [83]: s
Out[83]: 
0    a
1    b
2    a
dtype: category
Categories (4, object): ['a', 'b', 'c', 'd']

In [84]: s.cat.remove_unused_categories()
Out[84]: 
0    a
1    b
2    a
dtype: category
Categories (2, object): ['a', 'b']

重置cagtegory

使用set_categories()可以同时进行添加和删除category操作:


In [85]: s = pd.Series(["one", "two", "four", "-"], dtype="category")

In [86]: s
Out[86]: 
0     one
1     two
2    four
3       -
dtype: category
Categories (4, object): ['-', 'four', 'one', 'two']

In [87]: s = s.cat.set_categories(["one", "two", "three", "four"])

In [88]: s
Out[88]: 
0     one
1     two
2    four
3     NaN
dtype: category
Categories (4, object): ['one', 'two', 'three', 'four']

category排序

如果category创建的时候带有 ordered=True , 那么可以对其进行排序操作:


In [91]: s = pd.Series(["a", "b", "c", "a"]).astype(CategoricalDtype(ordered=True))

In [92]: s.sort_values(inplace=True)

In [93]: s
Out[93]: 
0    a
3    a
1    b
2    c
dtype: category
Categories (3, object): ['a' < 'b' < 'c']

In [94]: s.min(), s.max()
Out[94]: ('a', 'c')

可以使用 as_ordered() 或者 as_unordered() 来强制排序或者不排序:


In [95]: s.cat.as_ordered()
Out[95]: 
0    a
3    a
1    b
2    c
dtype: category
Categories (3, object): ['a' < 'b' < 'c']

In [96]: s.cat.as_unordered()
Out[96]: 
0    a
3    a
1    b
2    c
dtype: category
Categories (3, object): ['a', 'b', 'c']

重排序

使用Categorical.reorder_categories() 可以对现有的category进行重排序:


In [103]: s = pd.Series([1, 2, 3, 1], dtype="category")

In [104]: s = s.cat.reorder_categories([2, 3, 1], ordered=True)

In [105]: s
Out[105]: 
0    1
1    2
2    3
3    1
dtype: category
Categories (3, int64): [2 < 3 < 1]

多列排序

sort_values 支持多列进行排序:


In [109]: dfs = pd.DataFrame(
   .....:     {
   .....:         "A": pd.Categorical(
   .....:             list("bbeebbaa"),
   .....:             categories=["e", "a", "b"],
   .....:             ordered=True,
   .....:         ),
   .....:         "B": [1, 2, 1, 2, 2, 1, 2, 1],
   .....:     }
   .....: )
   .....: 

In [110]: dfs.sort_values(by=["A", "B"])
Out[110]: 
   A  B
2  e  1
3  e  2
7  a  1
6  a  2
0  b  1
5  b  1
1  b  2
4  b  2

比较操作

如果创建的时候设置了ordered==True ,那么category之间就可以进行比较操作。支持 ==, !=, >, >=, <, 和 <=这些操作符。


In [113]: cat = pd.Series([1, 2, 3]).astype(CategoricalDtype([3, 2, 1], ordered=True))

In [114]: cat_base = pd.Series([2, 2, 2]).astype(CategoricalDtype([3, 2, 1], ordered=True))

In [115]: cat_base2 = pd.Series([2, 2, 2]).astype(CategoricalDtype(ordered=True))
In [119]: cat > cat_base
Out[119]: 
0     True
1    False
2    False
dtype: bool

In [120]: cat > 2
Out[120]: 
0     True
1    False
2    False
dtype: bool

其他操作

Cagetory本质上来说还是一个Series,所以Series的操作category基本上都可以使用,比如: Series.min(), Series.max() 和 Series.mode()。

value_counts:


In [131]: s = pd.Series(pd.Categorical(["a", "b", "c", "c"], categories=["c", "a", "b", "d"]))

In [132]: s.value_counts()
Out[132]: 
c    2
a    1
b    1
d    0
dtype: int64

DataFrame.sum():


In [133]: columns = pd.Categorical(
   .....:     ["One", "One", "Two"], categories=["One", "Two", "Three"], ordered=True
   .....: )
   .....: 

In [134]: df = pd.DataFrame(
   .....:     data=[[1, 2, 3], [4, 5, 6]],
   .....:     columns=pd.MultiIndex.from_arrays([["A", "B", "B"], columns]),
   .....: )
   .....: 

In [135]: df.sum(axis=1, level=1)
Out[135]: 
   One  Two  Three
0    3    3      0
1    9    6      0

Groupby:


In [136]: cats = pd.Categorical(
   .....:     ["a", "b", "b", "b", "c", "c", "c"], categories=["a", "b", "c", "d"]
   .....: )
   .....: 

In [137]: df = pd.DataFrame({"cats": cats, "values": [1, 2, 2, 2, 3, 4, 5]})

In [138]: df.groupby("cats").mean()
Out[138]: 
      values
cats        
a        1.0
b        2.0
c        4.0
d        NaN

In [139]: cats2 = pd.Categorical(["a", "a", "b", "b"], categories=["a", "b", "c"])

In [140]: df2 = pd.DataFrame(
   .....:     {
   .....:         "cats": cats2,
   .....:         "B": ["c", "d", "c", "d"],
   .....:         "values": [1, 2, 3, 4],
   .....:     }
   .....: )
   .....: 

In [141]: df2.groupby(["cats", "B"]).mean()
Out[141]: 
        values
cats B        
a    c     1.0
     d     2.0
b    c     3.0
     d     4.0
c    c     NaN
     d     NaN

Pivot tables:


In [142]: raw_cat = pd.Categorical(["a", "a", "b", "b"], categories=["a", "b", "c"])

In [143]: df = pd.DataFrame({"A": raw_cat, "B": ["c", "d", "c", "d"], "values": [1, 2, 3, 4]})

In [144]: pd.pivot_table(df, values="values", index=["A", "B"])
Out[144]: 
     values
A B        
a c       1
  d       2
b c       3
  d       4

到此这篇关于Pandas数据类型之category的用法的文章就介绍到这了,更多相关category的用法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Pandas数据类型之category的用法

本文链接: https://www.lsjlt.com/news/11040.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Pandas数据类型之category的用法
    创建category 使用Series创建 在创建Series的同时添加dtype="category"就可以创建好category了。category分为两部分,一部分是order,一部分是字面量: In [1...
    99+
    2022-06-02
    category的用法 Python pandas
  • Pandas数据类型中category的用法
    本篇内容主要讲解“Pandas数据类型中category的用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Pandas数据类型中category的用法”吧!创建category使用Series...
    99+
    2023-06-20
  • pandas中如何创建category类型数据
    这篇文章主要介绍了pandas中如何创建category类型数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 T1、直接创建 category类型数据可知,在ca...
    99+
    2023-06-14
  • 详细介绍在pandas中创建category类型数据的几种方法
    在pandas中创建category类型数据的几种方法之详细攻略  T1、直接创建 category类型数据 可知,在category类型数据中,每一个元素的值要么是预设...
    99+
    2022-11-12
  • pandas数据类型之Series的具体使用
    目录Series类型Series的三种创建方式通过数组创建Series创建指定索引列的Series使用字典创建标量创建Series对象Series的常见操作Series的值访问访问整...
    99+
    2022-11-11
  • pandas中的series数据类型
    import pandas as pd import numpy as np import names ''' 写在前面的话: 1、series与array类型的不同之处为series有索引,而另一个没有;series中的数据...
    99+
    2023-01-30
    数据类型 pandas series
  • python数据处理之Pandas类型转换的实现
    目录转换为字符串类型转换为数值类型转为数值类型还可以使用to_numeric()函数分类数据(Category)数据类型小结转换为字符串类型 tips['sex_str'] = ti...
    99+
    2022-11-10
  • Pandas数据类型转换df.astype()及数据类型查看df.dtypes的使用
    目录1.数据框字段类型查看:df.dtypes2.维度查看df.shape:3.数据框的策略基本信息df.info():4.某一列格式df['列名'].dtype:5...
    99+
    2022-11-11
  • python数据处理之Pandas类型转换怎么实现
    这篇文章主要介绍“python数据处理之Pandas类型转换怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python数据处理之Pandas类型转换怎么实现”文章能帮助大家解决问题。转换为字...
    99+
    2023-06-30
  • Python 之 Pandas DataFrame 数据类型的简介、创建的列操作
    文章目录 一、DataFrame 结构简介二、DataFrame 对象创建1. 使用普通列表创建2. 使用嵌套列表创建3 指定数值元素的数据类型为 float4. 字典嵌套列表创建5. 添加自...
    99+
    2023-08-31
    pandas python 数据分析
  • python-pandas创建Series数据类型的操作
    1.什么是pandas 2.查看pandas版本信息 print(pd.__version__) 输出: 0.24.1 3.常见数据类型 常见的数据类型: - 一维: ...
    99+
    2022-11-12
  • Pandas中Series的创建及数据类型转换
    目录 一、实战场景二、主要知识点三、菜鸟实战1、创建 python 文件,用Numpy创建Series2、转换Series的数据类型 四、补充1、创建 pytho...
    99+
    2022-11-11
  • 【Python】【pandas】打印 DataFrame 的每一列数据类型。
    方法一: 可以使用 dtypes 属性来打印 DataFrame 的每一列数据类型。dtypes 属性返回一个 Series,其中包含每个列的名称和对应的数据类型。 以下是打印 DataFrame 每一列数据类型的示例代码: print(d...
    99+
    2023-09-04
    python pandas 开发语言
  • pandas中对文本类型数据的处理的方法有哪些
    本篇内容介绍了“pandas中对文本类型数据的处理的方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1.英文字母大小写转换及填充s&...
    99+
    2023-06-25
  • js数据类型之数字类型的示例分析
    这篇文章主要介绍了js数据类型之数字类型的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。我们具体介绍一下js的数据类型其中一种。一、...
    99+
    2022-10-19
  • pandas数据合并之pd.concat()用法详解
    目录一、简介二 、代码例1:上下堆叠拼接例2:axis=1 左右拼接一、简介 pd.concat()函数可以沿着指定的轴将多个dataframe或者series拼接到一起。...
    99+
    2022-11-11
  • pandas中对文本类型数据的处理小结
    目录1.英文字母大小写转换及填充2.字符串合并与拆分2.1 多列字符串合并2.2 一列 列表形式的文本合并为一列2.3 一列字符串与自身合并成为一列2.4 一列字符串拆分为多列2.4...
    99+
    2022-11-12
  • 怎么使用PHP数据类型之查看和判断数据类型
    这篇文章主要介绍怎么使用PHP数据类型之查看和判断数据类型,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!查看数据类型gettype(传入一个变量)等够获得变量的类型 var_dump(传入一个变量)输出变量的类型和值...
    99+
    2023-06-15
  • Pandas数据结构之Series的使用
    目录一. Series 简介二. 实例化 Series2.1 使用一维数组实例化2.2 使用字典实例化2.3 使用标量例化三.Series 简单使用3.1 为Series添加Name...
    99+
    2022-11-13
  • Python的基本数据类型之Number
    Python下载地址: https://www.python.org/downloads/ 部分参考资料:廖雪峰的网站 Python与Java在一定程度上比较相似,都是面向对象型的语言。首先搭配好Python的开发环境,网上相关...
    99+
    2023-01-31
    数据类型 Python Number
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作