pandas库入门之数据特征分析 北理工嵩天老师python数据分析与展示 单元8随堂笔记

pandas库入门之数据特征分析 北理工嵩天老师python数据分析与展示 单元8随堂笔记

1. 数据的排序

1.1 对一组数据的理解

一组数据表达一个或多个含义
摘要:在数据形成过程中一些结果。即有损地提取数据特诊的过程
通过摘要我们能获得数据的:
* 基本统计(含排序)
* 分布/累计统计
* 数据特征(相关性,周期性等)
* 数据挖掘(形成知识)

1.2 Pandas库的数据排序

.sort_index()方法在指定轴上根据索引进行排序,默认升序。
.sort_index(axis=0,ascending=True) ascending指递增排序。

import pandas as pd

import numpy as np

b = pd.DataFrame(np.arange(20).reshape(4,5),index=['c','a','d','b'])
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
c=b.sort_index()  #默认在0轴进行操作
c
0 1 2 3 4
a 5 6 7 8 9
b 15 16 17 18 19
c 0 1 2 3 4
d 10 11 12 13 14
c=c.sort_index(axis=1 ,ascending=False)
c
4 3 2 1 0
a 9 8 7 6 5
b 19 18 17 16 15
c 4 3 2 1 0
d 14 13 12 11 10

**.sort_values()方法在指定轴上根据数值进行排序,默认升序。
Series.sort_values(axis=0,ascending=True)
DataFrame.Sort_values(by,axis=0,ascending=True)
by: axis轴上的某个索引或索引列表

c = b.sort_values(2,ascending=False) #按第二列数据降序排列
c
0 1 2 3 4
b 15 16 17 18 19
d 10 11 12 13 14
a 5 6 7 8 9
c 0 1 2 3 4
c = c.sort_values('a',axis=1,ascending=False)
c
4 3 2 1 0
b 19 18 17 16 15
d 14 13 12 11 10
a 9 8 7 6 5
c 4 3 2 1 0

NaN统一放到排序的末尾

数据的基本统计分析

基本的统计分析函数,适用于Series和DataFrame类型

在这里插入图片描述在这里插入图片描述

适用于Series类型

在这里插入图片描述

import pandas as pd

a = pd.Series([9,8,7,6],index=['a','b','c','d'])
a
a    9
b    8
c    7
d    6
dtype: int64
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
type(a.describe())
pandas.core.series.Series
a.describe()['count']
4.0
a.describe()['max']
9.0
b = pd.DataFrame(np.arange(20).reshape(4,5),index=['c','a','d','b'])
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
type(b.describe())
pandas.core.frame.DataFrame
b.describe().ix['max']  #  以Series对象返回
D:\PYTHON\anaconda\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.





0    15.0
1    16.0
2    17.0
3    18.0
4    19.0
Name: max, dtype: float64

数据的累计统计分析

适用于Series 和 DataFrame类型

在这里插入图片描述

b = pd.DataFrame(np.arange(20).reshape(4,5),index=['c','a','d','b'])
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
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
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
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
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

适用于Series和DataFrame类型,滚动计算(窗口计算)。

依次计算w相邻的元素的统计值
在这里插入图片描述

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
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
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减小,两个变量负相关。
X增大,Y无视,两个变量不相关。

如何度量俩个变量的相关性?

协方差方法:

在这里插入图片描述
协方差>0,X和Y正相关。
协方差<0,X和Y负相关。
协方差=0,X和Y独立无关。

Peason相关系数

在这里插入图片描述
r的取值范围[-1,1].
|r|:
0.8-1.0 极强相关
0.6-0.8 强相关
0.4-0.6 中等程度相关
0.2-0.4 弱相关
0-0.2 极弱相关或不相关

相关分析函数

在这里插入图片描述

hprice = pd.Series([3.84,22.93,12.75,22.6,12.33] ,index=['2008','2009','2010','2011','2012'])

m2 = pd.Series([8.18,18.38,9.13,7.82,6.69],index=['2008','2009','2010','2011','2012'])

hprice.corr(m2)
0.5323702649465167
发布了15 篇原创文章 · 获赞 6 · 访问量 3280

猜你喜欢

转载自blog.csdn.net/supreme_1/article/details/100657482