pandas聚合aggregate
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/5/24 15:03
# @Author : zhang chao
# @File : s.py
import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2019', periods=10),
columns = ['A', 'B', 'C', 'D']) print (df)
print("=======================================")
r = df.rolling(window=3,min_periods=1)
print (r)
print("=======================================")
print("r.aggregate(np.sum)")
print (r.aggregate(np.sum))
print("=======================================")
print("r['A'].aggregate(np.sum)")
print (r['A'].aggregate(np.sum))
print("=======================================")
print("r[['A','B']].aggregate(np.sum)")
print (r[['A','B']].aggregate(np.sum))
print("=======================================")
print("r['A'].aggregate([np.sum,np.mean])")
print (r['A'].aggregate([np.sum,np.mean]))
print("=======================================")
print("r.aggregate({'A' : np.sum,'B' : np.mean})")
print (r.aggregate({'A' : np.sum,'B' : np.mean}))
print("=======================================")
print("r[['A','B']].aggregate([np.sum,np.mean]")
print (r[['A','B']].aggregate([np.sum,np.mean])) D:\Download\python3\python3.exe D:/Download/pycharmworkspace/s.py
A B C D
2019-01-01 0.744560 0.208652 0.542045 -0.995837
2019-01-02 0.029809 -1.419936 -0.461988 2.177032
2019-01-03 0.613583 1.515249 0.256546 -0.973564
2019-01-04 0.124320 1.152804 0.152107 1.629035
2019-01-05 -0.287906 1.003523 -0.793393 0.231969
2019-01-06 -0.045296 -0.921622 0.894335 0.773035
2019-01-07 -0.695347 0.512206 0.208833 0.953205
2019-01-08 -1.197178 0.142301 -0.854875 -1.044017
2019-01-09 -2.352468 0.047127 -0.351634 -0.373885
2019-01-10 0.678406 0.500947 0.304250 -0.606522
=======================================
Rolling [window=3,min_periods=1,center=False,axis=0]
=======================================
r.aggregate(np.sum)
A B C D
2019-01-01 0.744560 0.208652 0.542045 -0.995837
2019-01-02 0.774369 -1.211283 0.080057 1.181195
2019-01-03 1.387952 0.303966 0.336603 0.207631
2019-01-04 0.767712 1.248117 -0.053335 2.832504
2019-01-05 0.449996 3.671576 -0.384740 0.887441
2019-01-06 -0.208882 1.234705 0.253049 2.634040
2019-01-07 -1.028549 0.594107 0.309775 1.958209
2019-01-08 -1.937820 -0.267115 0.248293 0.682223
2019-01-09 -4.244992 0.701633 -0.997676 -0.464698
2019-01-10 -2.871239 0.690374 -0.902259 -2.024425
=======================================
r['A'].aggregate(np.sum)
2019-01-01 0.744560
2019-01-02 0.774369
2019-01-03 1.387952
2019-01-04 0.767712
2019-01-05 0.449996
2019-01-06 -0.208882
2019-01-07 -1.028549
2019-01-08 -1.937820
2019-01-09 -4.244992
2019-01-10 -2.871239
Freq: D, Name: A, dtype: float64
=======================================
r[['A','B']].aggregate(np.sum)
A B
2019-01-01 0.744560 0.208652
2019-01-02 0.774369 -1.211283
2019-01-03 1.387952 0.303966
2019-01-04 0.767712 1.248117
2019-01-05 0.449996 3.671576
2019-01-06 -0.208882 1.234705
2019-01-07 -1.028549 0.594107
2019-01-08 -1.937820 -0.267115
2019-01-09 -4.244992 0.701633
2019-01-10 -2.871239 0.690374
=======================================
r['A'].aggregate([np.sum,np.mean])
sum mean
2019-01-01 0.744560 0.744560
2019-01-02 0.774369 0.387185
2019-01-03 1.387952 0.462651
2019-01-04 0.767712 0.255904
2019-01-05 0.449996 0.149999
2019-01-06 -0.208882 -0.069627
2019-01-07 -1.028549 -0.342850
2019-01-08 -1.937820 -0.645940
2019-01-09 -4.244992 -1.414997
2019-01-10 -2.871239 -0.957080
=======================================
r.aggregate({'A' : np.sum,'B' : np.mean})
B A
2019-01-01 0.208652 0.744560
2019-01-02 -0.605642 0.774369
2019-01-03 0.101322 1.387952
2019-01-04 0.416039 0.767712
2019-01-05 1.223859 0.449996
2019-01-06 0.411568 -0.208882
2019-01-07 0.198036 -1.028549
2019-01-08 -0.089038 -1.937820
2019-01-09 0.233878 -4.244992
2019-01-10 0.230125 -2.871239
=======================================
r[['A','B']].aggregate([np.sum,np.mean]
A B
sum mean sum mean
2019-01-01 0.744560 0.744560 0.208652 0.208652
2019-01-02 0.774369 0.387185 -1.211283 -0.605642
2019-01-03 1.387952 0.462651 0.303966 0.101322
2019-01-04 0.767712 0.255904 1.248117 0.416039
2019-01-05 0.449996 0.149999 3.671576 1.223859
2019-01-06 -0.208882 -0.069627 1.234705 0.411568
2019-01-07 -1.028549 -0.342850 0.594107 0.198036
2019-01-08 -1.937820 -0.645940 -0.267115 -0.089038
2019-01-09 -4.244992 -1.414997 0.701633 0.233878
2019-01-10 -2.871239 -0.957080 0.690374 0.230125 Process finished with exit code 0
pandas聚合aggregate的更多相关文章
- mongodb的聚合aggregate|group|match|project|sort|limit|skip|unwind
聚合 aggregate 聚合(aggregate)主要用于计算数据,类似sql中的sum().avg() 语法 db.集合名称.aggregate([{管道:{表达式}}]) 管道 管道在Unix和 ...
- MongoDB(七):聚合aggregate
1. 聚合aggregate 聚合主要用于计算数据,类似sql中的sum().avg() 语法: db.集合名称.aggregate([{管道:{表达式}}]) stu准备的数据: db.stu.in ...
- mongodb 聚合(aggregate)
MongoDB中文手册|官方文档中文版 https://docs.mongoing.com/ 聚合操作处理数据记录和 return 计算结果.聚合操作将来自多个文档的值组合在一起,并且可以对分组数 ...
- pandas聚合和分组运算——GroupBy技术(1)
数据聚合与分组运算——GroupBy技术(1),有需要的朋友可以参考下. pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作.根据一个或多个 ...
- Pandas聚合
数据聚合 import pandas as pd from pandas import Series import numpy as np # 准备数据 df = pd.DataFrame([[-0. ...
- pandas聚合和分组运算之groupby
pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作.根据一个或多个键(可以是函数.数组或DataFrame列名)拆分pandas对象.计算分 ...
- django中聚合aggregate和annotate GROUP BY的使用方法
接触django已经很长时间了,但是使用QuerySet查询集的方式一直比较低端,只会使用filter/Q函数/exclude等方式来查询,数据量比较小的时候还可以,但是如果数据量很大,而且查询比较复 ...
- MongoDB聚合(aggregate)
一.基础 1.什么是聚合? 聚合是基于数据处理的聚合管道,每个文档通过一个有多个阶段(stage)组成的管道可以对每个阶段的管道进行分组.过滤等功能,然后经过一系列的处理,输出相应的结果 db.集合名 ...
- pandas 聚合求和等操作
参考:https://blog.csdn.net/m0_38139979/article/details/106606633 result1= result.groupby(['user_id', ' ...
随机推荐
- mysql-修改字段类型和修改字段名称
文章转自:https://blog.csdn.net/u010002184/article/details/79354136 mysql修改字段类型: --能修改字段类型.类型长度.默认值.注释 -- ...
- MyBatis+Hibernate+JDBC对比分析
MyBatis目前作为持久层,用的最多,因为它符合互联网开发的变动性,实际开发中需求总会有这样的,那样的变动,MyBatis虽然没有Hibernate那么全自动化,而且对于开发人员的sql能力要求比较 ...
- parallel方法 异步并行执行
Promise提供了all方法, 但是状态只有2种, 第一种是所有promise实例都成功则返回值组成一个数组,传递给p的回调函数: 第二种是有一个实例被rejected,状态就变成rejected, ...
- "system:serviceaccount:rook-ceph-system:rook-ceph-system" cannot get pods/log in the namespace "rook-ceph"
1.kubectl logs -f rook-ceph-operator-86776bbc44-cv5hs -n rook-ceph-system 日志:E | op-cluster: unk ...
- python属性查找 深入理解(attribute lookup)
在Python中,属性查找(attribute lookup)是比较复杂的,特别是涉及到描述符descriptor的时候. 在上一文章末尾,给出了一段代码,就涉及到descriptor与attribu ...
- 通过重建清理SVN服务器无用目录,不丢失其他目录修改记录
1.主要时有时间希望调整一些文件的目录结构,或者移除一个大量占用空间的文件节省服务器磁盘,但是又不希望调整后,对应的修改记录丢失.这时可以通过服务器目录重建实现. 2.重建后只是被排除掉的目录的修改记 ...
- 51NOD1522 上下序列/CF567F Mausoleum DP
题目传送门:http://codeforces.com/problemset/problem/567/F 大致题意:你有$1$到$N$的所有正整数每个数两个,现在需要你将它排成一个序列,使得序列为单峰 ...
- Topshelf的Ioc实现
在前面使用Topshelf的文章里,我们的工作类TownCrier使用的是无参数的构造函数,满足测试的目的.在实际的开发过程中,我们常常需要使用带有参数的构造函数,就不可避免的使用Ioc的技术.在这里 ...
- P1438 无聊的数列
P1438 无聊的数列 链接 分析: 等差数列可加,首项相加,公差相加. 代码: #include<cstdio> #include<algorithm> #include&l ...
- 并行编程(Parallel Framework)
前言 并行编程:通过编码方式利用多核或多处理器称为并行编程,多线程概念的一个子集. 并行处理:把正在执行的大量的任务分割成小块,分配给多个同时运行的线程.多线程的一种. 并行编程分为如下几个结构: 1 ...