Pandas Statistical Functions
import pandas as pd
import random
import numpy as np
n_rows=5
n_cols=2
df = pd.DataFrame(np.random.randn(n_rows, n_cols),
index = pd.date_range('1/1/2000', periods=n_rows),
columns = ['A','B'])
df=df.apply(lambda x:[int(xx*10) for xx in x],axis=0)
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18 | 3 |
| 2000-01-02 | 5 | -4 |
| 2000-01-03 | -2 | 8 |
| 2000-01-04 | 0 | 1 |
| 2000-01-05 | -18 | 3 |
pct_change
## pct_change() to compute the percent change over a given number of periods
df.pct_change(periods=1) # b{t}=(a{t}-a{t-1})/a{t-1}
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | NaN | NaN |
| 2000-01-02 | -1.277778 | -2.333333 |
| 2000-01-03 | -1.400000 | -3.000000 |
| 2000-01-04 | -1.000000 | -0.875000 |
| 2000-01-05 | -inf | 2.000000 |
df.pct_change(periods=2) # b{t}=(a{t}-a{t-2})/a{t-2}
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | NaN | NaN |
| 2000-01-02 | NaN | NaN |
| 2000-01-03 | -0.888889 | 1.666667 |
| 2000-01-04 | -1.000000 | -1.250000 |
| 2000-01-05 | 8.000000 | -0.625000 |
Covariance
df.cov()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| A | 114.80 | -17.85 |
| B | -17.85 | 18.70 |
df.A.cov(df.B)
-17.849999999999998
Correlation
df.corr()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| A | 1.000000 | -0.385253 |
| B | -0.385253 | 1.000000 |
Data ranking
df.rank()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | 1.5 | 3.5 |
| 2000-01-02 | 5.0 | 1.0 |
| 2000-01-03 | 3.0 | 5.0 |
| 2000-01-04 | 4.0 | 2.0 |
| 2000-01-05 | 1.5 | 3.5 |
df.rank(axis=1)
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | 1.0 | 2.0 |
| 2000-01-02 | 2.0 | 1.0 |
| 2000-01-03 | 1.0 | 2.0 |
| 2000-01-04 | 1.0 | 2.0 |
| 2000-01-05 | 1.0 | 2.0 |
method parameter:
average : average rank of tied group
min : lowest rank in the group
max : highest rank in the group
first : ranks assigned in the order they appear in the array
Window Functions
cumsum
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18 | 3 |
| 2000-01-02 | 5 | -4 |
| 2000-01-03 | -2 | 8 |
| 2000-01-04 | 0 | 1 |
| 2000-01-05 | -18 | 3 |
df.cumsum()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18 | 3 |
| 2000-01-02 | -13 | -1 |
| 2000-01-03 | -15 | 7 |
| 2000-01-04 | -15 | 8 |
| 2000-01-05 | -33 | 11 |
rolling
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18 | 3 |
| 2000-01-02 | 5 | -4 |
| 2000-01-03 | -2 | 8 |
| 2000-01-04 | 0 | 1 |
| 2000-01-05 | -18 | 3 |
r=df.rolling(window=2)
r.mean()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | NaN | NaN |
| 2000-01-02 | -6.5 | -0.5 |
| 2000-01-03 | 1.5 | 2.0 |
| 2000-01-04 | -1.0 | 4.5 |
| 2000-01-05 | -9.0 | 2.0 |
r.count()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | 1.0 | 1.0 |
| 2000-01-02 | 2.0 | 2.0 |
| 2000-01-03 | 2.0 | 2.0 |
| 2000-01-04 | 2.0 | 2.0 |
| 2000-01-05 | 2.0 | 2.0 |
r.max()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | NaN | NaN |
| 2000-01-02 | 5.0 | 3.0 |
| 2000-01-03 | 5.0 | 8.0 |
| 2000-01-04 | 0.0 | 8.0 |
| 2000-01-05 | 0.0 | 3.0 |
| Method | Description |
|---|---|
| count() | Number of non-null observations |
| sum() | Sum of values |
| mean() | Mean of values |
| median() | Arithmetic median of values |
| min() | Minimum |
| max() | Maximum |
| std() | Bessel-corrected sample standard deviation |
| var() | Unbiased variance |
| skew() | Sample skewness (3rd moment) |
| kurt() | Sample kurtosis (4th moment) |
| quantile() | Sample quantile (value at %) |
| apply() | Generic apply |
| cov() | Unbiased covariance (binary) |
| corr() | Correlation (binary) |
win_type can specify distribution function.
parameter 'on' to specify a column (rather than the default of the index) in a DataFrame.
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18 | 3 |
| 2000-01-02 | 5 | -4 |
| 2000-01-03 | -2 | 8 |
| 2000-01-04 | 0 | 1 |
| 2000-01-05 | -18 | 3 |
df.rolling(window='3d',min_periods=3).sum() ## 最近三天
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | NaN | NaN |
| 2000-01-02 | NaN | NaN |
| 2000-01-03 | -15.0 | 7.0 |
| 2000-01-04 | 3.0 | 5.0 |
| 2000-01-05 | -20.0 | 12.0 |
expanding
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18 | 3 |
| 2000-01-02 | 5 | -4 |
| 2000-01-03 | -2 | 8 |
| 2000-01-04 | 0 | 1 |
| 2000-01-05 | -18 | 3 |
df.expanding().mean() ## statistic with all data up to a point in time
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18.00 | 3.000000 |
| 2000-01-02 | -6.50 | -0.500000 |
| 2000-01-03 | -5.00 | 2.333333 |
| 2000-01-04 | -3.75 | 2.000000 |
| 2000-01-05 | -6.60 | 2.200000 |
Exponentially Weighted Windows(ewm)
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18 | 3 |
| 2000-01-02 | 5 | -4 |
| 2000-01-03 | -2 | 8 |
| 2000-01-04 | 0 | 1 |
| 2000-01-05 | -18 | 3 |
df.ewm(alpha=0.9).mean()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 2000-01-01 | -18.000000 | 3.000000 |
| 2000-01-02 | 2.909091 | -3.363636 |
| 2000-01-03 | -1.513514 | 6.873874 |
| 2000-01-04 | -0.151215 | 1.586859 |
| 2000-01-05 | -16.215282 | 2.858699 |
Pandas Statistical Functions的更多相关文章
- 小白学 Python 数据分析(2):Pandas (一)概述
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 概览 首先还是几个官方链接放一下: Pandas 官网:https://pandas.pydata.or ...
- Why Apache Spark is a Crossover Hit for Data Scientists [FWD]
Spark is a compelling multi-purpose platform for use cases that span investigative, as well as opera ...
- Scipy教程 - 统计函数库scipy.stats
http://blog.csdn.net/pipisorry/article/details/49515215 统计函数Statistical functions(scipy.stats) Pytho ...
- scipy.stats
scipy.stats Scipy的stats模块包含了多种概率分布的随机变量,随机变量分为连续的和离散的两种.所有的连续随机变量都是rv_continuous的派生类的对象,而所有的离散随机变量都是 ...
- GitHub相关资料&&可以参加的开源项目
GitHub相关的资料 有不懂的地方时可以看GitHub Docs. GitHub tutorial GitHub glossary GitHub的字典,可以看到里面特定的概念. All about ...
- R实战 第十二篇:随机数
由R生成的随机数实际上伪随机数,也就是说,随机数是由某种算法而不是真正的随机过程产生的,随机数生成器需要一个初始值来生成数字,该初始值叫做种子.通过把种子设置为特定的值,可以保证每次运行同一段代码时都 ...
- PP图和QQ图
一. QQ图 分位数图示法(Quantile Quantile Plot,简称 Q-Q 图) 统计学里Q-Q图(Q代表分位数)是一个概率图,用图形的方式比较两个概率分布,把他们 ...
- D01-R语言基础学习
R语言基础学习——D01 20190410内容纲要: 1.R的下载与安装 2.R包的安装与使用方法 (1)查看已安装的包 (2)查看是否安装过包 (3)安装包 (4)更新包 3.结果的重用 4.R处理 ...
- matlab toolboxes 大全
MATLAB Toolboxes top (Top) Audio - Astronomy - BiomedicalInformatics - Chemometrics - Chaos - Chemi ...
随机推荐
- 1057 数零壹 (20 分)C语言
给定一串长度不超过 10^5的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例 ...
- 从零开始Go语言-GoLand(编译器)-Windows(平台)
本文章适合那些想入门Go语言,却又不知道如何搭建自己的第一个HelloWorld的同学. 推荐几个Go语言相关学习网站: C语言中文网: http://c.biancheng.net/golang/ ...
- 微信小程序开发笔记(二)
一.前言 继承上一篇所说的,有了对微信小程序的基础概念后,这边将会示范动手做一个小程序,在动手的过程中我们可以更快的熟悉小程序里面的架构和开发流程. 二.小程序的设计 这次要做的是一个猜数字的程序,程 ...
- Using TFRecords and tf.Example
-----这篇其实是TensorFlow的官方tutorials,由于没有翻译,笔者姑且翻译一下,用来日后思考.------- 原址:https://www.tensorflow.org/tutori ...
- 从头学pytorch(十六):VGG NET
VGG AlexNet在Lenet的基础上增加了几个卷积层,改变了卷积核大小,每一层输出通道数目等,并且取得了很好的效果.但是并没有提出一个简单有效的思路. VGG做到了这一点,提出了可以通过重复使⽤ ...
- Netty快速入门(05)Java NIO 介绍-Selector
Java NIO Selector Selector是Java NIO中的一个组件,用于检查一个或多个NIO Channel的状态是否处于可读.可写.如此可以实现单线程管理多个channels,也就是 ...
- spring get方法 中文(UTF-8)乱码
问题: 前端用Get方法进行如下请求: 在浏览器中输入:http://localhost:8080/dmaList/ExportBySQL?sql=&names=分区级别&size=1 ...
- 通过自己实现接口来加深理解SpringMVC的执行流程
功能介绍 上篇文章[从源码角度了解SpringMVC的执行流程]通过接口源码向大家介绍了SpringMVC的执行流程,主要偏重于源码.这篇文件我们来自己实现那几个关键接口,来真实体验下SpringMV ...
- dp - 逆序数序列
对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的 数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样自然数数 ...
- java 常用锁
公平锁和非公平锁 1.公平锁,是指多个线程按照申请的顺序来获取锁,类似排队打饭,先来后到. 2.非公平锁,是指多个线程获取锁的顺序并不是按照申请锁的顺序,有可能后申请的线程 比先申请的线程优先获取锁, ...