Pandas如何使用GroupBy分组
发表于:2024-10-25 作者:千家信息网编辑
千家信息网最后更新 2024年10月25日,这篇文章主要介绍 Pandas如何使用GroupBy分组,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!groupby对象import pandas as pdimport nu
千家信息网最后更新 2024年10月25日Pandas如何使用GroupBy分组
这篇文章主要介绍 Pandas如何使用GroupBy分组,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
groupby对象
import pandas as pdimport numpy as npdf = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C' : np.random.randn(8), 'D' : np.random.randn(8)})gb.groupby('A')print(df.groupby('A'))In [26]: gb. gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transformgb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.vargb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight
分组迭代Iterating through groups
In [41]: grouped = df.groupby('A')In [42]: for name, group in grouped: ....: print(name) ....: print(group) ....: bar A B C D1 bar one -0.042379 -0.0893293 bar three -0.009920 -0.9458675 bar two 0.495767 1.956030foo A B C D0 foo one -0.919854 -1.1313452 foo two 1.247642 0.3378634 foo two 0.290213 -0.9321326 foo one 0.362949 0.0175877 foo three 1.548106 -0.016692
获得一个分组get_group
In [44]: grouped.get_group('bar')Out[44]: A B C D1 bar one -0.042379 -0.0893293 bar three -0.009920 -0.9458675 bar two 0.495767 1.956030
使用多种函数agg()
相同的函数
In [56]: grouped = df.groupby('A')In [57]: grouped['C'].agg([np.sum, np.mean, np.std])Out[57]: sum mean stdA bar 0.443469 0.147823 0.301765foo 2.529056 0.505811 0.966450
不同的函数
In [60]: grouped.agg({'C' : np.sum, ....: 'D' : lambda x: np.std(x, ddof=1)}) ....: Out[60]: C DA bar 0.443469 1.490982foo 2.529056 0.645875
转变数据框transformation
转变函数(transform)中需要返回一个和分组块(group chunk)同样大小的结果,比如我们需要标准化每一个分组的数据:
In [66]: index = pd.date_range('10/1/1999', periods=1100)In [67]: ts = pd.Series(np.random.normal(0.5, 2, 1100), index)In [68]: ts = ts.rolling(window=100,min_periods=100).mean().dropna()In [71]: key = lambda x: x.year#使用年来分组In [72]: zscore = lambda x: (x - x.mean()) / x.std()#标准化In [73]: transformed = ts.groupby(key).transform(zscore)#使用索引的年份来分组,然后标准化各组数据In [80]: compare = pd.DataFrame({'Original': ts, 'Transformed': transformed})# 做出图形
过滤Filtration
filter方法返回一个子集(subset)。比如我们只想要组长度大于2的分组:
In [105]: dff = pd.DataFrame({'A': np.arange(8), 'B': list('aabbbbcc')})In [106]: dff.groupby('B').filter(lambda x: len(x) > 2)Out[106]: A B2 2 b3 3 b4 4 b5 5 b
灵活运用apply
In [123]: dfOut[123]: A B C D0 foo one -0.919854 -1.1313451 bar one -0.042379 -0.0893292 foo two 1.247642 0.3378633 bar three -0.009920 -0.9458674 foo two 0.290213 -0.9321325 bar two 0.495767 1.9560306 foo one 0.362949 0.0175877 foo three 1.548106 -0.016692In [124]: grouped = df.groupby('A')# could also just call .describe()In [125]: grouped['C'].apply(lambda x: x.describe())Out[125]: A bar count 3.000000 mean 0.147823 std 0.301765 min -0.042379 25% -0.026149 50% -0.009920 75% 0.242924... foo mean 0.505811 std 0.966450 min -0.919854 25% 0.290213 50% 0.362949 75% 1.247642 max 1.548106Name: C, dtype: float64
以上是" Pandas如何使用GroupBy分组"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
分组
函数
数据
标准
标准化
内容
篇文章
不同
相同
价值
兴趣
图形
多种
大小
子集
对象
小伙
小伙伴
年份
年来
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发专业好还是网页好
家用linux服务器配置推荐
计算机网络技术学校招生
宏大软件开发
深圳c语言软件开发哪家便宜
江苏统一软件开发以客为尊
数据库选择题必考
浩盈互联网科技有限公司
政务网络安全宣传年度总结
快手直播服务器维护中是什么意思
真实的服务器视频
lol是啥软件开发的
软件开发的语言有多少种
共筑网络安全与治理论坛
中搜网络技术有限公司股票
内江软件开发
采用啥子技术连接数据库
网页怎样连接数据库
湖北学校时钟同步服务器
昌吉互联网科技怎么样
幼儿园网络安全保障实施方案
虚机部署服务器需要冗余吗
c 对数据库数据的查找
如何制定软件开发奖金
数据库索引有哪几种
数据库一个表可以有几个主码
数据库中indexes
pubg不小心改了服务器怎么办
软件开发需要学习的知识
初中生网络安全教育培训内容