数据分析-Pandas类别数据序列Union操作

数据分析-Pandas类别数据序列Union操作

数据分析和处理中,难免会遇到各种数据,那么数据呈现怎样的规律呢?不管金融数据,风控数据,营销数据等等,莫不如此。如何通过图示展示数据的规律?

数据表,时间序列数据在数据分析建模中很常见,例如天气预报,空气状态监测,股票交易等金融场景。数据分析过程中重新调整,重塑数据表是很重要的技巧,此处选择Titanic数据,以及巴黎、伦敦欧洲城市空气质量监测 N O 2 NO_2 NO2数据作为样例。

数据分析

数据分析-Pandas如何转换产生新列

数据分析-Pandas如何统计数据概况

数据分析-Pandas如何轻松处理时间序列数据

数据分析-Pandas如何选择数据子集

数据分析-Pandas如何重塑数据表-CSDN博客

实验数据分析处理,股票序列,时间序列,信号序列,有时候表格的数据并不完全是数值类型,也有可能是字符串,或者其他数据,需要做分类处理。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实战系列-简单聊聊LangChain

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(二)

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(一)

GPT实战系列-ChatGLM2模型的微调训练参数解读

GPT实战系列-如何用自己数据微调ChatGLM2模型训练

GPT实战系列-ChatGLM2部署Ubuntu+Cuda11+显存24G实战方案

GPT实战系列-Baichuan2本地化部署实战方案

GPT实战系列-Baichuan2等大模型的计算精度与量化

GPT实战系列-GPT训练的Pretraining,SFT,Reward Modeling,RLHF

GPT实战系列-探究GPT等大模型的文本生成-CSDN博客

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex_StarSky

你的鼓励是创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值