# 【4.2.1】Pandas中常用的统计分析

## 一.数据的基本统计分析

.sum() 计算数据的总和，按0轴计算，下同
.count() 非NaN值的数量
.mean() .median() 计算数据的算术平均值、算术中位数
.var() .std() 计算数据的方差、标准差
.min() .max() 计算数据的最小值、最大值
.argmin() .argmax() 计算数据最大值、最小值所在位置的索引位置(自动索引)
.idxmin() .idxmax() 计算数据最大值、最小值所在位置的索引(自定义索引)
.describe() 针对0轴(各列)的统计汇总

import pandas as pd
import numpy as np

a= pd.Series([9,8,7,6],index=['a','b','c','d'])
print a
a    9
b    8
c    7
d    6
dtype: int64

print a.describe()
count    4.000000
mean     7.500000
std      1.290994
min      6.000000
25%      6.750000
50%      7.500000
75%      8.250000
max      9.000000
dtype: float64

print type(a.describe())
<class 'pandas.core.series.Series'>

print a.describe()['count']
4.0

print a.describe()['max']
9.0

import pandas as pd
import numpy as np
b = pd.DataFrame(np.arange(20).reshape(4,5),index=['c','a','d','b'])

print b
0   1   2   3   4
c   0   1   2   3   4
a   5   6   7   8   9
d  10  11  12  13  14
b  15  16  17  18  19

print b.describe()
0          1          2          3          4
count   4.000000   4.000000   4.000000   4.000000   4.000000
mean    7.500000   8.500000   9.500000  10.500000  11.500000
std     6.454972   6.454972   6.454972   6.454972   6.454972
min     0.000000   1.000000   2.000000   3.000000   4.000000
25%     3.750000   4.750000   5.750000   6.750000   7.750000
50%     7.500000   8.500000   9.500000  10.500000  11.500000
75%    11.250000  12.250000  13.250000  14.250000  15.250000
max    15.000000  16.000000  17.000000  18.000000  19.000000

print type(b.describe())
<class 'pandas.core.frame.DataFrame'>

print b.describe().ix['max']
0    15.0
1    16.0
2    17.0
3    18.0
4    19.0
Name: max, dtype: float64

print b.describe()[2]
count     4.000000
mean      9.500000
std       6.454972
min       2.000000
25%       5.750000
50%       9.500000
75%      13.250000
max      17.000000
Name: 2, dtype: float64

## 二、数据的累计分布

.cumsum() 依次给出前1、2、...、n个数的和
.cumprod() 依次给出前1、2、...、n个数的积
.cummax() 依次给出前1、2、...、n个数的最大值
.cummin() 依次给出前1、2、...、n个数的最小值

import pandas as pd
import numpy as np
b = pd.DataFrame(np.arange(20).reshape(4,5),index=['c','a','d','b'])

print b
0   1   2   3   4
c   0   1   2   3   4
a   5   6   7   8   9
d  10  11  12  13  14
b  15  16  17  18  19

print b.cumsum()
0   1   2   3   4
c   0   1   2   3   4
a   5   7   9  11  13
d  15  18  21  24  27
b  30  34  38  42  46

print b.cumprod()
0     1     2     3     4
c  0     1     2     3     4
a  0     6    14    24    36
d  0    66   168   312   504
b  0  1056  2856  5616  9576

print b.cummin()
0  1  2  3  4
c  0  1  2  3  4
a  0  1  2  3  4
d  0  1  2  3  4
b  0  1  2  3  4

print b.cummax()
0   1   2   3   4
c   0   1   2   3   4
a   5   6   7   8   9
d  10  11  12  13  14
b  15  16  17  18  19

.rolling(w).sum() 依次计算相邻w个元素的和
.rolling(w).mean() 依次计算相邻w个元素的算术平均值
.rolling(w).var() 依次计算相邻w个元素的方差
.rolling(w).std() 依次计算相邻w个元素的标准差
.rolling(w).min() .max() 依次计算相邻w个元素的最小值和最大值

print b.rolling(2).sum()
0     1     2     3     4
c   NaN   NaN   NaN   NaN   NaN
a   5.0   7.0   9.0  11.0  13.0
d  15.0  17.0  19.0  21.0  23.0
b  25.0  27.0  29.0  31.0  33.0

print b.rolling(3).sum()
0     1     2     3     4
c   NaN   NaN   NaN   NaN   NaN
a   NaN   NaN   NaN   NaN   NaN
d  15.0  18.0  21.0  24.0  27.0
b  30.0  33.0  36.0  39.0  42.0

## 三.数据的相关分析

• X增大，Y增大，两个变量正相关
• X增大，Y减小，两个变量负相关
• X增大，Y无视，两个变量不相关

• 协方差>0, X和Y正相关
• 协方差<0, X和Y负相关
• 协方差=0, X和Y独立无关

Pearson相关系数

r取值范围[‐1,1]

• 0.8‐1.0 极强相关
• 0.6‐0.8 强相关
• 0.4‐0.6 中等程度相关
• 0.2‐0.4 弱相关
• 0.0‐0.2 极弱相关或无相关

.cov() 计算协方差矩阵
.corr() 计算相关系数矩阵, Pearson、Spearman、Kendall等系数

import pandas as pd
import numpy as np

a1 = pd.Series([3,4,6,8],index = ['a','b','c','d'])
a2 = pd.Series([4,5,7,8],index = ['a','b','c','d'])

print a1.corr(a2)
0.988064363511