数据分析-Pandas类别数据序列Union操作
数据分析和处理中,难免会遇到各种数据,那么数据呈现怎样的规律呢?不管金融数据,风控数据,营销数据等等,莫不如此。如何通过图示展示数据的规律?
数据表,时间序列数据在数据分析建模中很常见,例如天气预报,空气状态监测,股票交易等金融场景。数据分析过程中重新调整,重塑数据表是很重要的技巧,此处选择Titanic数据,以及巴黎、伦敦欧洲城市空气质量监测 N O 2 NO_2 NO2数据作为样例。
数据分析
实验数据分析处理,股票序列,时间序列,信号序列,有时候表格的数据并不完全是数值类型,也有可能是字符串,或者其他数据,需要做分类处理。pandas如何控制数据分类处理呢?需要配置哪些参数?
类型的Union联合
如果要合并不一定相同类别的两个分类数据,函数union_categoricals 将列表部分合并,重新组合两者类别列表。
In [1]: from pandas.api.types import union_categoricals
In [2]: a = pd.Categorical(["b", "c"])
In [3]: b = pd.Categorical(["a", "b"])
In [5]: union_categoricals([a, b])
Out[5]:
['b', 'c', 'a', 'b']
Categories (3, object): ['b', 'c', 'a']
默认情况下,重组生成的类别将按它们出现的先后顺序排序,如果你希望类别是词法重新排序,可以使用参数: sort_categories=True
In [6]: union_categoricals([a, b], sort_categories=True)
Out[6]:
['b', 'c', 'a', 'b']
Categories (3, object): ['a', 'b', 'c']
当然,union_categoricals 也适用于相同类别的类别和有序 的“简单”情况:
In [7]: a = pd.Categorical(["a", "b"], ordered=True)
In [8]: b = pd.Categorical(["a", "b", "a"], ordered=True)
In [9]: union_categoricals([a, b])
Out[9]:
['a', 'b', 'a', 'b', 'a']
Categories (2, object): ['a' < 'b']
但是,当类别是有序的,如果两者不完全相同,就会报错TypeError
In [1]: a = pd.Categorical(["a", "b"], ordered=True)
In [2]: b = pd.Categorical(["a", "b", "c"], ordered=True)
In [3]: union_categoricals([a, b])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[205], line 1
----> 1 union_categoricals([a, b])
File ~/work/pandas/pandas/pandas/core/dtypes/concat.py:341, in union_categoricals(to_union, sort_categories, ignore_order)
339 if all(c.ordered for c in to_union):
340 msg = "to union ordered Categoricals, all categories must be the same"
--> 341 raise TypeError(msg)
342 raise TypeError("Categorical.ordered must be the same")
344 if ignore_order:
TypeError: to union ordered Categoricals, all categories must be the same
此时,如果仍需要合并,具有不同类别或顺序的有序分类数据,可以通过使用参数解决:ignore_ordered=True
In [5]: a = pd.Categorical(["a", "b", "c"], ordered=True)
In [6]: b = pd.Categorical(["c", "b", "a"], ordered=True)
In [7]: union_categoricals([a, b], ignore_order=True)
Out[7]:
['a', 'b', 'c', 'c', 'b', 'a']
Categories (3, object): ['a', 'b', 'c']
union_categoricals 用于无序分类数据时,按数组合并,类别的合并是重新生成。
In [1]: a = pd.Series(["b", "c"], dtype="category")
In [2]: b = pd.Series(["a", "b"], dtype="category")
In [3]: union_categoricals([a, b])
Out[3]:
['b', 'c', 'a', 'b']
Categories (3, object): ['b', 'c', 'a']
类别合并的序号变化
union_categoricals
在合并时,可以对类别的整数代码进行重新编码。这正是你所知的, 但是,如果你的类别操作,基于依赖于类别的确切编号,需要知晓这种合并处理结果。
In [212]: c1 = pd.Categorical(["b", "c"])
In [213]: c2 = pd.Categorical(["a", "b"])
In [214]: c1
Out[214]:
['b', 'c']
Categories (2, object): ['b', 'c']
# "b" is coded to 0
In [215]: c1.codes
Out[215]: array([0, 1], dtype=int8)
In [216]: c2
Out[216]:
['a', 'b']
Categories (2, object): ['a', 'b']
# "b" is coded to 1
In [217]: c2.codes
Out[217]: array([0, 1], dtype=int8)
In [218]: c = union_categoricals([c1, c2])
In [219]: c
Out[219]:
['b', 'c', 'a', 'b']
Categories (3, object): ['b', 'c', 'a']
# "b" is coded to 0 throughout, same as c1, different from c2
In [220]: c.codes
Out[220]: array([0, 1, 2, 0], dtype=int8)
以上代码只是一个简单示例,示例代码中的表达式可以根据实际问题进行修改。
后面介绍下其他的展示形式。
觉得有用 收藏 收藏 收藏
点个赞 点个赞 点个赞
End
GPT专栏文章:
GPT实战系列-ChatGLM3本地部署CUDA11+1080Ti+显卡24G实战方案
GPT实战系列-LangChain + ChatGLM3构建天气查询助手
GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手
GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(二)
GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(一)
GPT实战系列-ChatGLM2部署Ubuntu+Cuda11+显存24G实战方案