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', ' ...
随机推荐
- 利用Apache AXIS 1 发布WebService
首先,AXIS 是apache的作品. 1.到apache去下载相关的包,包括axis.jar.axis-ant.jar.commons-discovery-0.2.jar等.将jar包放到WEB-I ...
- go标准库的学习-encoding/xml
参考:https://studygolang.com/pkgdoc 导入方式: import "encoding/xml" 实现的简单的理解XML命名空间的XML 1.0编译器 f ...
- https://leetcode.com/problems/palindromic-substrings/description/
https://www.cnblogs.com/grandyang/p/7404777.html 博客中写的<=2,实际上<=1也是可以的 相当于判断一个大指针内所有子字符串是否可能为回文 ...
- JVM实践
package com.lsw.classloader; import java.io.FileInputStream;import java.lang.reflect.Field;import ja ...
- day90
Vue项目简介 最终效果:Vue通过axios发请求给Django后台,Django返回数据给Vue 创建项目: 创建vue项目: -安装node.js -vue脚手架 -vue create 项目名 ...
- IIS 8的第一次请求不变慢如何配置
首先需要在Window中添加Application Initialization 在IIS中配置Application Pool 在IIS配置Web Site 配置完成,如果版本在7.5,可以下载:A ...
- 初识Identity(一)
一.Authentication(身份验证)和Authorization(授权) 如何构建安全的web应用?authentication和authorization是asp.net中最重要最基本的防护 ...
- mvn打包到私服的命令
1.mvn clean package install -Dmaven.test.skip=true deploy 2.docker清楚Nexus私服上包的命令: a) docker exec -it ...
- React-性能优化pureComponent
每当store里有数据更新时,render()函数就会执行,有时候store的更新数据与本组件并没有关系,render()不必执行. 我们可以用shouldComponentUpdate来优化组件. ...
- flask-socketio笔记
Flask-SocketIO使Flask应用程序可以访问客户端和服务器之间的低延迟双向通信. 客户端应用程序可以使用Javascript,C ++,Java和Swift中的任何SocketIO官方客户 ...