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的更多相关文章

  1. 小白学 Python 数据分析(2):Pandas (一)概述

    人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 概览 首先还是几个官方链接放一下: Pandas 官网:https://pandas.pydata.or ...

  2. 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 ...

  3. Scipy教程 - 统计函数库scipy.stats

    http://blog.csdn.net/pipisorry/article/details/49515215 统计函数Statistical functions(scipy.stats) Pytho ...

  4. scipy.stats

    scipy.stats Scipy的stats模块包含了多种概率分布的随机变量,随机变量分为连续的和离散的两种.所有的连续随机变量都是rv_continuous的派生类的对象,而所有的离散随机变量都是 ...

  5. GitHub相关资料&&可以参加的开源项目

    GitHub相关的资料 有不懂的地方时可以看GitHub Docs. GitHub tutorial GitHub glossary GitHub的字典,可以看到里面特定的概念. All about ...

  6. R实战 第十二篇:随机数

    由R生成的随机数实际上伪随机数,也就是说,随机数是由某种算法而不是真正的随机过程产生的,随机数生成器需要一个初始值来生成数字,该初始值叫做种子.通过把种子设置为特定的值,可以保证每次运行同一段代码时都 ...

  7. PP图和QQ图

     一. QQ图      分位数图示法(Quantile Quantile Plot,简称 Q-Q 图)       统计学里Q-Q图(Q代表分位数)是一个概率图,用图形的方式比较两个概率分布,把他们 ...

  8. D01-R语言基础学习

    R语言基础学习——D01 20190410内容纲要: 1.R的下载与安装 2.R包的安装与使用方法 (1)查看已安装的包 (2)查看是否安装过包 (3)安装包 (4)更新包 3.结果的重用 4.R处理 ...

  9. matlab toolboxes 大全

    MATLAB Toolboxes top (Top) Audio - Astronomy - BiomedicalInformatics - Chemometrics  - Chaos - Chemi ...

随机推荐

  1. UCI 人口收入数据分析(python)

    一.项目介绍 UCI上有许多免费的数据集可以拿来练习,可以在下面的网站找寻 http://archive.ics.uci.edu/ml/datasets.html 这次我使用的是人口收入调查,里面会有 ...

  2. 传统远程注入线程,加载DLL

    代码根据<windows黑客编程技术详解>来的   远程DLL注入:把我们的恶意DLL强制注入到正常的进程中   每个程序执行时都会调用kernal32.dll,加载DLL时,通过Load ...

  3. C# DataTable数据类型判断

    当我们从数据中获取到数据,一般会使用 DataTable 接收,然后会遍历每行数据.由于从数据库中读取的数据可能为空,比如我们的编译代码如下: foreach (DataRow datarow in ...

  4. invalid expression: missing ) after argument list in xxx 或者 console.error(("[Vue warn]: " + msg + trace));

    效果图:   此处错误原因   中文输入法的 逗号 导致    :   解决方案: 改为 英文输入法的 逗号

  5. 1.PL/SQL Developer的快捷键

    设置步骤: Configure  =>  preference => 用户界面 => 编辑器  => 自动替换 => 启用 => 编辑 =>保存(产生一个文件 ...

  6. 我与Git的那些破事系列(下)--分支模型

    在上篇文章中,我提到了Git的基本概念和一些本人实际项目中的总结.然而,最近读了一片Vincent Driessen写的一篇文章,觉得他总结的太好了,忍不住站在他的肩膀上写一篇自己的理解.文章的连接放 ...

  7. 原生JS在网页上复制的所有文字后面自动加上一段版权声明

    不少技术博客有这样的处理,当我们复制代码的时候,会自动加上一段本信息版权为XXXX,这是怎么实现的呢? 其实实现的方式很简单,可以在我的网站页面上绑定一个copy事件,当你复制文章内容的时候,自动在剪 ...

  8. 三分钟带你入门GitHub

    一,首先,我们来说一下什么是GitHub GitHub是一个基于git打造的开源社区 ,同时也是一个大型同性交友平台 ,作为一个专业的程序员,你非常有必要知道并使用GitHub:作为一个国际化社区,所 ...

  9. [bzoj2186] [洛谷P2155] [Sdoi2008] 沙拉公主的困惑

    Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...

  10. 机器学习环境配置系列二之cuDNN

    1.下载cuDNN 前往: NVIDIA cuDNN home page. 进入下载 勾选Nvidia的协议复选框(流氓的选择,不勾选不能下载) 选择与安装的cuda版本一致的cudnn进行下载. 2 ...