Pandas | 16 聚合
当有了滚动,扩展和ewm
对象创建了以后,就有几种方法可以对数据执行聚合。
DataFrame应用聚合
可以通过向整个DataFrame传递一个函数来进行聚合,或者通过标准的获取项目方法来选择一个列。
在整个数据框上应用聚合
import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2000', periods=10),
columns = ['A', 'B', 'C', 'D'])
print(df)
print('\n') r = df.rolling(window=3,min_periods=1)
print(r)
print('\n') print(r.aggregate(np.sum))
输出结果:
A B C D
2000-01-01 1.081883 -1.133242 -0.477461 0.669900
2000-01-02 -1.120673 -0.889724 0.232907 0.391879
2000-01-03 -0.050530 -0.213853 0.100309 0.296723
2000-01-04 0.165836 -0.015513 -1.008884 -1.877693
2000-01-05 0.210501 -1.395490 -0.495589 -0.072882
2000-01-06 -0.639261 -2.301506 0.703845 -0.867376
2000-01-07 -0.225980 0.684229 0.985126 0.763059
2000-01-08 -0.748013 1.274504 -0.195817 2.293899
2000-01-09 -1.683620 -1.466185 0.491427 -1.895749
2000-01-10 0.842794 1.598099 0.843714 0.777707 Rolling [window=3,min_periods=1,center=False,axis=0] A B C D
2000-01-01 1.081883 -1.133242 -0.477461 0.669900
2000-01-02 -0.038790 -2.022966 -0.244553 1.061778
2000-01-03 -0.089320 -2.236820 -0.144245 1.358501
2000-01-04 -1.005367 -1.119090 -0.675668 -1.189091
2000-01-05 0.325807 -1.624856 -1.404165 -1.653851
2000-01-06 -0.262924 -3.712509 -0.800629 -2.817951
2000-01-07 -0.654740 -3.012767 1.193381 -0.177199
2000-01-08 -1.613253 -0.342773 1.493154 2.189581
2000-01-09 -2.657613 0.492548 1.280736 1.161209
2000-01-10 -1.588839 1.406418 1.139325 1.175857
在数据框的单个列上应用聚合
import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2000', periods=10),
columns = ['A', 'B', 'C', 'D'])
print (df)
print("\n")
r = df.rolling(window=3,min_periods=1)
print (r['A'].aggregate(np.sum))
输出结果:
A B C D
2000-01-01 -1.095530 -0.415257 -0.446871 -1.267795
2000-01-02 -0.405793 -0.002723 0.040241 -0.131678
2000-01-03 -0.136526 0.742393 -0.692582 -0.271176
2000-01-04 0.318300 -0.592146 -0.754830 0.239841
2000-01-05 -0.125770 0.849980 0.685083 0.752720
2000-01-06 1.410294 0.054780 0.297992 -0.034028
2000-01-07 0.463223 -1.239204 -0.056420 0.440893
2000-01-08 -2.244446 -0.516937 -2.039601 -0.680606
2000-01-09 0.991139 0.026987 -2.391856 0.585565
2000-01-10 0.112228 -0.701284 -1.139827 1.484032
2000-01-01 -1.095530
2000-01-02 -1.501323
2000-01-03 -1.637848
2000-01-04 -0.224018
2000-01-05 0.056004
2000-01-06 1.602824
2000-01-07 1.747747
2000-01-08 -0.370928
2000-01-09 -0.790084
2000-01-10 -1.141079
Freq: D, Name: A, dtype: float64
在DataFrame的多列上应用聚合
import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2018', periods=10),
columns = ['A', 'B', 'C', 'D'])
print (df)
print ("\n")
r = df.rolling(window=3,min_periods=1)
print (r[['A','B']].aggregate(np.sum))
输出结果:
A B C D
2018-01-01 0.518897 0.988917 0.435691 -1.005703
2018-01-02 1.793400 0.130314 2.313787 0.870057
2018-01-03 -0.297601 0.504137 -0.951311 -0.146720
2018-01-04 0.282177 0.142360 -0.059013 0.633174
2018-01-05 2.095398 -0.153359 0.431514 -1.185657
2018-01-06 0.134847 0.188138 0.828329 -1.035120
2018-01-07 0.780541 0.138942 -1.001229 0.714896
2018-01-08 0.579742 -0.642858 0.835013 -1.504110
2018-01-09 -1.692986 -0.861327 -1.125359 0.006687
2018-01-10 -0.263689 1.182349 -0.916569 0.617476
A B
2018-01-01 0.518897 0.988917
2018-01-02 2.312297 1.119232
2018-01-03 2.014697 1.623369
2018-01-04 1.777976 0.776811
2018-01-05 2.079975 0.493138
2018-01-06 2.512422 0.177140
2018-01-07 3.010786 0.173722
2018-01-08 1.495130 -0.315777
2018-01-09 -0.332703 -1.365242
2018-01-10 -1.376932 -0.321836
在DataFrame的单个列上应用多个函数 (用列表包裹多个函数)
import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('2019/01/01', periods=10),
columns = ['A', 'B', 'C', 'D'])
print (df) print("\n") r = df.rolling(window=3,min_periods=1)
print (r['A'].aggregate([np.sum,np.mean]))
输出结果:
A B C D
2019-01-01 1.022641 -1.431910 0.780941 -0.029811
2019-01-02 -0.302858 0.009886 -0.359331 -0.417708
2019-01-03 -1.396564 0.944374 -0.238989 -1.873611
2019-01-04 0.396995 -1.152009 -0.560552 -0.144212
2019-01-05 -2.513289 -1.085277 -1.016419 -1.586994
2019-01-06 -0.513179 0.823411 0.670734 1.196546
2019-01-07 -0.363239 -0.991799 0.587564 -1.100096
2019-01-08 1.474317 1.265496 -0.216486 -0.224218
2019-01-09 2.235798 -1.381457 -0.950745 -0.209564
2019-01-10 -0.061891 -0.025342 0.494245 -0.081681
sum mean
2019-01-01 1.022641 1.022641
2019-01-02 0.719784 0.359892
2019-01-03 -0.676780 -0.225593
2019-01-04 -1.302427 -0.434142
2019-01-05 -3.512859 -1.170953
2019-01-06 -2.629473 -0.876491
2019-01-07 -3.389707 -1.129902
2019-01-08 0.597899 0.199300
2019-01-09 3.346876 1.115625
2019-01-10 3.648224 1.216075
在DataFrame的多列上应用多个函数
import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('2020/01/01', periods=10),
columns = ['A', 'B', 'C', 'D']) print (df)
print("\n")
r = df.rolling(window=3,min_periods=1)
print (r[['A','B']].aggregate([np.sum,np.mean]))
输出结果:
A B C D
2020-01-01 1.053702 0.355985 0.746638 -0.233968
2020-01-02 0.578520 -1.171843 -1.764249 -0.709913
2020-01-03 -0.491185 0.975212 0.200139 -3.372621
2020-01-04 -1.331328 0.776316 0.216623 0.202313
2020-01-05 -1.023147 -0.913686 1.457512 0.999232
2020-01-06 0.995328 -0.979826 -1.063695 0.057925
2020-01-07 0.576668 1.065767 -0.270744 -0.513707
2020-01-08 0.520258 0.969043 -0.119177 -0.125620
2020-01-09 -0.316480 0.549085 1.862249 1.091265
2020-01-10 0.461321 -0.368662 -0.988323 0.543011
A B
sum mean sum mean
2020-01-01 1.053702 1.053702 0.355985 0.355985
2020-01-02 1.632221 0.816111 -0.815858 -0.407929
2020-01-03 1.141037 0.380346 0.159354 0.053118
2020-01-04 -1.243993 -0.414664 0.579686 0.193229
2020-01-05 -2.845659 -0.948553 0.837843 0.279281
2020-01-06 -1.359146 -0.453049 -1.117195 -0.372398
2020-01-07 0.548849 0.182950 -0.827744 -0.275915
2020-01-08 2.092254 0.697418 1.054985 0.351662
2020-01-09 0.780445 0.260148 2.583896 0.861299
2020-01-10 0.665099 0.221700 1.149466 0.383155
将不同的函数应用于DataFrame的不同列
import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(3, 4),
index = pd.date_range('2020/01/01', periods=3),
columns = ['A', 'B', 'C', 'D'])
print (df)
print("\n")
r = df.rolling(window=3,min_periods=1)
print (r.aggregate({'A' : np.sum,'B' : np.mean}))
输出结果:
A B C D
2020-01-01 -0.246302 -0.057202 0.923807 -1.019698
2020-01-02 0.285287 1.467206 -0.368735 -0.397260
2020-01-03 -0.163219 -0.401368 1.254569 0.580188
A B
2020-01-01 -0.246302 -0.057202
2020-01-02 0.038985 0.705002
2020-01-03 -0.124234 0.336212
Pandas | 16 聚合的更多相关文章
- Pandas 分组聚合 :分组、分组对象操作
1.概述 1.1 group语法 df.groupby(self, by=None, axis=0, level=None, as_index: bool=True, sort: bool=True, ...
- Python Pandas分组聚合
Pycharm 鼠标移动到函数上,CTRL+Q可以快速查看文档,CTR+P可以看基本的参数. apply(),applymap()和map() apply()和applymap()是DataFrame ...
- pandas的聚合操作: groupyby与agg
pandas提供基于行和列的聚合操作,groupby可理解为是基于行的,agg则是基于列的 从实现上看,groupby返回的是一个DataFrameGroupBy结构,这个结构必须调用聚合函数(如su ...
- Pandas 分组聚合
# 导入相关库 import numpy as np import pandas as pd 创建数据 index = pd.Index(data=["Tom", "Bo ...
- pandas之聚合运算
通过聚合运算可以得到我们比较感兴趣的数据以方便处理 import pandas as pd import numpy as np # 先创建一组数据表DataFrame df = pd.DataFra ...
- pandas分组聚合案例
美国2012年总统候选人政治献金数据分析 导入包 import numpy as np import pandas as pd from pandas import Series,DataFrame ...
- pandas:聚合统计、数据分箱、分组可视化
1.聚合统计 1.1描述统计 #df.describe(),对数据的总体特征进行描述 df.groupby('team').describe() df.groupby('team').describe ...
- DataAnalysis-Pandas分组聚合
title: Pandas分组聚合 tags: 数据分析 python categories: DataAnalysis toc: true date: 2020-02-10 16:28:49 Des ...
- 快速上手pandas(下)
和上文一样,先导入后面会频繁使用到的模块: In [1]: import numpy as np import pandas as pd import matplotlib.pyplot as p ...
随机推荐
- sync 异步编程
using System; using System.Net; using System.Threading; using System.Threading.Tasks; namespace Cons ...
- Unity Shader 序列帧动画
shader中的序列帧动画属于纹理动画中的一种,主要原理是将给定的纹理进行等分,再根据时间的变化循环播放等分中的一部分. Unity Shader 内置时间变量 名称 类型 描述 _Time floa ...
- [转帖]mDNS原理的简单理解
mDNS原理的简单理解 https://binkery.com/archives/318.html 发现还有avahi-daemon mdns 设置ip地址 等等事项 网络部分 自己学习的还是不够多 ...
- secure-file-priv特性
转载自:https://segmentfault.com/a/1190000009333563 当出现:1290 - The MySQL server is running with the --se ...
- 《 .NET并发编程实战》阅读指南 - 第11章
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- HTML5新标签和CSS伪类
HTML5提供了很多新的标签,由于HTML5的兼容性比较差,HTML5的标签常用于手机端 <nav> <footer> <section> <header&g ...
- 新一代ActiveMQ —— Apache ActiveMQ Artemis
资料: .net demo : https://github.com/apache/activemq-artemis/tree/master/examples/protocols/amqp/dotne ...
- Python - Win10系统下Python3.x环境配置
Win10系统下Python3.x环境配置 https://blog.csdn.net/qq_41952474/article/details/82630551
- 《Real World Haskell》内容脉络整理
p.s. 其实就是28–(6入门[1~4,6])-(10暂不用[17,20~28])=13网页啊! 1~ 6 章: 语法入门 (类型,函数,表达式语法糖,typeclass) 7~13章: 熟练/ ...
- cpio建立、还原备份档
1. 简介 加入.解开cpio或tar备份档内的文件 与tar相似,将文件归档到硬盘或磁带等存储设备中 2. tar比较 在所处理的文件类型方面,它比tar更全面,但也更复杂 cpio比tar更为可靠 ...