Pandas数据特征分析
对一组数据的理解(摘要:有损的提取数据特征的过程):
1. 基本统计(含排序)
2. 分布/累计统计
3. 数据特征:相关性、周期性等
4. 数据挖掘(形成知识)
Pandas库的数据排序:
.sort_index()方法在指定轴上根据索引进行排序,默认升序。
.sort_index(axis=0,ascending=True)
1 import numpy as np 2 import pandas as pd 3 4 b=pd.DataFrame(np.arange(20).reshape(4,5), index=['a','b','c','d']) 5 6 b 7 Out[4]: 8 0 1 2 3 4 9 a 0 1 2 3 4 10 b 5 6 7 8 9 11 c 10 11 12 13 14 12 d 15 16 17 18 19 13 14 b.sort_index(axis=0,ascending=True) 15 Out[5]: 16 0 1 2 3 4 17 a 0 1 2 3 4 18 b 5 6 7 8 9 19 c 10 11 12 13 14 20 d 15 16 17 18 19 21 22 b=pd.DataFrame(np.arange(20).reshape(4,5), index=['d','b','c','a']) 23 24 b.sort_index(axis=0,ascending=True) 25 Out[7]: 26 0 1 2 3 4 27 a 15 16 17 18 19 28 b 5 6 7 8 9 29 c 10 11 12 13 14 30 d 0 1 2 3 4
1 c=b.sort_index(axis=1,ascending=False) #对1轴进行排序 2 3 c 4 Out[15]: 5 4 3 2 1 0 6 d 4 3 2 1 0 7 b 9 8 7 6 5 8 c 14 13 12 11 10 9 a 19 18 17 16 15 10 11 c.sort_index() 12 Out[16]: 13 4 3 2 1 0 14 a 19 18 17 16 15 15 b 9 8 7 6 5 16 c 14 13 12 11 10 17 d 4 3 2 1 0
.sort_values()方法在制定轴上根据数值进行排序,默认升序。
Series.sort_values(axis=0,ascending=True)
DataFrame.sort_values(by,axis=0,ascending=True) # by: axis轴上的某个索引或索引列表
b.sort_values('b',axis=1,ascending=False) Out[18]: 4 3 2 1 0 d 4 3 2 1 0 b 9 8 7 6 5 c 14 13 12 11 10 a 19 18 17 16 15 b.sort_values(2,axis=0,ascending=False) Out[20]: 0 1 2 3 4 a 15 16 17 18 19 c 10 11 12 13 14 b 5 6 7 8 9 d 0 1 2 3 4 a=pd.DataFrame(np.arange(12).reshape(2,6),index=['a','b']) c=a+b c Out[34]: 0 1 2 3 4 5 a 15.0 17.0 19.0 21.0 23.0 NaN b 11.0 13.0 15.0 17.0 19.0 NaN c NaN NaN NaN NaN NaN NaN d NaN NaN NaN NaN NaN NaN c.sort_values(2) Out[36]: 0 1 2 3 4 5 b 11.0 13.0 15.0 17.0 19.0 NaN # NaN永远放在排序末尾 a 15.0 17.0 19.0 21.0 23.0 NaN c NaN NaN NaN NaN NaN NaN d NaN NaN NaN NaN NaN NaN
基本的统计分析函数
适用于Series和DataFrame类型
# Series只有0轴,所以DataFrame肯定是默认0轴计算
适用于Series类型
# 数据的默认索引一直都是在的,可以根据默认索引进行排序和切片。
适用于Series和DataFrame类型
# 将统计值一次性以Series或DataFrame数据类型的方式列出来
1 import pandas as pd 2 3 a=pd.Series([9,8,7,6],index=['a','b','c','d']) 4 6 a.describe() 7 Out[51]: 8 count 4.000000 9 mean 7.500000 10 std 1.290994 11 min 6.000000 12 25% 6.750000 13 50% 7.500000 14 75% 8.250000 15 max 9.000000 16 dtype: float64 17 18 a.describe()['count'] 19 Out[52]: 4.0 20 21 b 22 Out[61]: 23 0 1 2 3 4 24 0 0 1 2 3 4 25 1 5 6 7 8 9 26 2 10 11 12 13 14 27 3 15 16 17 18 19 28 29 b.describe() 30 Out[62]: 31 0 1 2 3 4 32 count 4.000000 4.000000 4.000000 4.000000 4.000000 33 mean 7.500000 8.500000 9.500000 10.500000 11.500000 34 std 6.454972 6.454972 6.454972 6.454972 6.454972 35 min 0.000000 1.000000 2.000000 3.000000 4.000000 36 25% 3.750000 4.750000 5.750000 6.750000 7.750000 37 50% 7.500000 8.500000 9.500000 10.500000 11.500000 38 75% 11.250000 12.250000 13.250000 14.250000 15.250000 39 max 15.000000 16.000000 17.000000 18.000000 19.000000 40 41 b.describe().ix['max'] # 跟Series的不一样了 42 __main__:1: DeprecationWarning: 43 .ix is deprecated. Please use 44 .loc for label based indexing or 45 .iloc for positional indexing 46 47 See the documentation here: 48 http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated 49 Out[63]: 50 0 15.0 51 1 16.0 52 2 17.0 53 3 18.0 54 4 19.0 55 Name: max, dtype: float64 56 57 b.describe()[2] 58 Out[64]: 59 count 4.000000 60 mean 9.500000 61 std 6.454972 62 min 2.000000 63 25% 5.750000 64 50% 9.500000 65 75% 13.250000 66 max 17.000000 67 Name: 2, dtype: float64
累计统计分析函数
适用于Series和DataFrame类型
# 沿着列方向
1 b 2 Out[66]: 3 0 1 2 3 4 4 0 0 1 2 3 4 5 1 5 6 7 8 9 6 2 10 11 12 13 14 7 3 15 16 17 18 19 8 9 b.cumsum() 10 Out[67]: 11 0 1 2 3 4 12 0 0 1 2 3 4 13 1 5 7 9 11 13 14 2 15 18 21 24 27 15 3 30 34 38 42 46 16 17 b.cumprod() 18 Out[68]: 19 0 1 2 3 4 20 0 0 1 2 3 4 21 1 0 6 14 24 36 22 2 0 66 168 312 504 23 3 0 1056 2856 5616 9576 24 25 b.cummax() 26 Out[69]: 27 0 1 2 3 4 28 0 0 1 2 3 4 29 1 5 6 7 8 9 30 2 10 11 12 13 14 31 3 15 16 17 18 19 32 33 b.cummin() 34 Out[70]: 35 0 1 2 3 4 36 0 0 1 2 3 4 37 1 0 1 2 3 4 38 2 0 1 2 3 4 39 3 0 1 2 3 4
适用于Series和DataFrame类型,滚动计算(窗口计算)。
1 b 2 Out[71]: 3 0 1 2 3 4 4 0 0 1 2 3 4 5 1 5 6 7 8 9 6 2 10 11 12 13 14 7 3 15 16 17 18 19 8 9 b.rolling(2).sum() 10 Out[72]: 11 0 1 2 3 4 12 0 NaN NaN NaN NaN NaN 13 1 5.0 7.0 9.0 11.0 13.0 14 2 15.0 17.0 19.0 21.0 23.0 15 3 25.0 27.0 29.0 31.0 33.0 16 17 b.rolling(2).std() 18 Out[73]: 19 0 1 2 3 4 20 0 NaN NaN NaN NaN NaN 21 1 3.535534 3.535534 3.535534 3.535534 3.535534 22 2 3.535534 3.535534 3.535534 3.535534 3.535534 23 3 3.535534 3.535534 3.535534 3.535534 3.535534
相关分析
统计学上的度量标准:协方差
两个事物,表示为X和Y:
协方差>0,X和Y正相关;
协方差<0,X和Y负相关;
协方差=0,X和Y独立无关;
Pearson相关系数
# r 取值范围[-1,1]
0.8-1:极强相关
0.6-0.8:强相关
0.4-0.6:中等程度相关
0.2-0.4:弱相关
0.0-0.2:极弱相关或不相关
相关分析函数
适用于Series和DataFrame
1 b=pd.Series([3.25,6.15,6.25,1.25,9.28]) 2 3 a=pd.Series([3.24,2.03,5.26,6.48,7.23],) 4 5 a.corr(b) 6 Out[84]: 0.1547009556096695
# 只针对pandas数据

浙公网安备 33010602011771号