pandas是python下常用来进行大数据处理与分析,本质是数理统计,所以本章简单了解一下pandas的一些统计函数,这里以series为例。

sum函数

sum函数可以统计series数值之和。

import pandas as pd
idx =  "hello the cruel world".split()
val = [1000, 201, None, 104]
t = pd.Series(val, index = idx)
print t, "<- t"
print t.sum()

mean函数

mean函数可以得到均值μμ,这时需要注意的是如果values里含有NaN,可以使用mean函数的参数避开NaN,默认情况下启用了skipna=True避开NaN值,如果需要考虑NaN可以使skipna=False,那么均值里是考虑了NaN项的,实际工作中是忽略掉的。

import pandas as pd
idx =  "hello the cruel world".split()
val = [1000, 201, None, 104]
t = pd.Series(val, index = idx)
print t, "<- t"
print t.mean()
print t.mean(skipna=False)

quantile分位数函数

分位数是统计学里的概念,可自行查找学习。

import pandas as pd
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
t = pd.Series(val, index = idx)
print t, "<- t"
print t.quantile()
print t.quantile(0.5)
print t.quantile(0.25)
print t.quantile(0.75)

describe函数

describe可以给出一系列的和统计相关的数据信息。

import pandas as pd
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
t = pd.Series(val, index = idx)
print t, "<- t"
print t.describe()

程序执行结果

count       4.000000
mean      409.500000
std       404.699477
min       104.000000
25%       176.750000
50%       267.000000
75%       499.750000
max      1000.000000
dtype: float64

max和idxmax函数

max函数可以返回series里最大值,而idxmax返回的是其index或者label。

import pandas as pd
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
t = pd.Series(val, index = idx)
print t, "<- t"
print t.max()
print t.idxmax()

程序执行结果:

hello    1000
the       201
cruel     333
world     104
dtype: int64 <- t
1000
hello

同样的还有min和idxmin两个函数。

统计学里的方差相关的函数

  • var函数计算方差,方差Variance反映的是模型每一次输出结果与模型输出期望(平均值)之间的误差,即模型的稳定性,在pandas的series里可以用var函数计算。
import pandas as pd
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
t = pd.Series(val, index = idx)
print t, "<- t"
print t.var(), "\t<- var"

程序执行结果:

hello    1000
the       201
cruel     333
world     104
dtype: int64 <- t
163781.66666666666  <- var

方差的计算公式如下:

这里的μμ是均值可以通过mean函数得到。所以可以通过python来验证一下var函数是否满足上边的公式的计算,

import pandas as pd
import numpy as np
idx =  "hello the cruel world".split()
val =  [1000, 201, 333, 104]
t = pd.Series(val, index = idx)
print t, "<- t"
print t.var(), "\t<- var"
x =  val
mu = t.mean()
y = [np.square(v - mu) for v in x]
print np.sum(y) / 3

程序的执行结果:

hello    1000
the       201
cruel     333
world     104
dtype: int64 <- t
163781.66666666666  <- var
163781.66666666666

import pandas as pd
import numpy as np
idx =  "hello the cruel world".split()
val =  [1000, 201, 333, 104]
t = pd.Series(val, index = idx)
print t, "<- t"
print t.var(), "\t<- var"
x =  val
mu = t.mean()
y = [np.square(v - mu) for v in x]
delta2 = np.sum(y) / 3
print delta2
print np.sqrt(delta2)
print t.std(), "\t<- std"

程序的执行结果:

hello    1000
the       201
cruel     333
world     104
dtype: int64 <- t
163781.66666666666  <- var
163781.66666666666
404.6994769784941
404.6994769784941   <- std

import pandas as pd
import numpy as np
idx =  "hello the cruel world".split()
val =  [1000, 201, 333, 104]
t = pd.Series(val, index = idx)
x =  val
mu = t.mean()
y = [np.abs(v - mu) for v in x]
md = np.sum(y) / 4
print md
print t.mad(), "\t<- mad"

程序执行结果:

295.25
295.25  <- mad

import pandas as pd
import numpy as np
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
x = pd.Series(val, index = idx)
van = [1100, 221, 303, 84]
y = pd.Series(van, index = idx)
xt =  val
mux = x.mean()
yt = van
muy = y.mean()
xx = [v - mux for v in xt]
yy = [v - muy for v in yt]
print xx
print yy
print np.sum(np.array(xx).dot(np.array(yy))) / 3
print x.cov(y), "\t<- cov"

程序执行结果:

[590.5, -208.5, -76.5, -305.5]
[673.0, -206.0, -124.0, -343.0]
184876.66666666666
184876.66666666666  <- cov

import pandas as pd
import numpy as np
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
x = pd.Series(val, index = idx)
van = [1100, 221, 303, 84]
y = pd.Series(van, index = idx)
xt =  val
mux = x.mean()
yt = van
muy = y.mean()
xx = [v - mux for v in xt]
yy = [v - muy for v in yt]
xx2 = [np.square(v - mux) for v in xt]
yy2 = [np.square(v - muy) for v in yt]
cov = np.sum(np.array(xx).dot(np.array(yy)))
muxy = np.sqrt(np.sum(xx2)) * np.sqrt(np.sum(yy2))
print cov / muxy
print x.corr(y), "\t<- corr"

程序执行结果:

0.998149178876946
0.9981491788769461  <- corr

1). 利用kurt计算峰度值。

import pandas as pd
import numpy as np
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
x = pd.Series(val, index = idx)
n = 4
mu = x.mean()
delta = x.std()
xu = [np.power((v - mu), 4) for v in val]
print (1.0 * n *(n + 1))/ ((n-1)*(n-2)*(n-3)) * np.sum(xu) / delta ** 4 - 3.0 * (n - 1) ** 2 / (n-2)*(n-3) , "<-python"
print x.kurt(), "<- kurt"

程序执行结果:

2.93023293658 <-python
2.93023293658 <- kurt

2). skew函数可以偏态值。

import pandas as pd
import numpy as np
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
x = pd.Series(val, index = idx)
n = 4
mu = x.mean()
delta = x.std()
xu = [np.power((v - mu), 3) for v in val]
print (1.0 * n) / ((n - 1)*(n - 2))*np.sum(xu) / delta ** 3, "<- python"
print x.skew(), "<- skew"

程序执行结果:

1.68850911034 <- python
1.68850911034 <- skew

统计学里的累计函数

import pandas as pd
idx =  "hello the cruel world".split()
val = [1000, 201, 333, 104]
t = pd.Series(val, index = idx)
print t, "<- t"print t.cumsum(), "\t<- cumsum"
print t.cumprod(), "\t<- cumprod"
print t.cummin(), "\t<- cummin"

程序执行结果:

hello    1000
the       201
cruel     333
world     104
dtype: int64 <- t
hello    1000
the      1201
cruel    1534
world    1638
dtype: int64    <- cumsum
hello          1000
the          201000
cruel      66933000
world    6961032000
dtype: int64    <- cumprod
hello    1000
the       201
cruel     201
world     104
dtype: int64    <- cummin

Pandas的Series统计函数(7)相关推荐

  1. pandas 遍历 series

    pandas 遍历 series from pandas import Series index1 = ['01', '02', '03', '04', '05', '06'] data1 = ['小 ...

  2. dataframe python格式_python3.6 pandas,Series和DataFrame基础格式与用法,附代码实例

    pandas 是基于numpy构建的库,加上numpy,主要用于科学运算和数据处理. 也是一个让我忘记昂贵的MATLAB,并且不得不复习SQL的库.. 一般引入规定: In [105]: from p ...

  3. pythonpandas函数详解_对pandas中Series的map函数详解

    Series的map方法可以接受一个函数或含有映射关系的字典型对象. 使用map是一种实现元素级转换以及其他数据清理工作的便捷方式. (DataFrame中对应的是applymap()函数,当然Dat ...

  4. dataframe两个表合并_Part25:Pandas基础(Series,DataFrame类的创建、索引、切片、算术方法)...

    一.为什么学习pandas numpy已经可以帮助我们进行数据的处理了,那么学习pandas的目的是什么呢? numpy能够帮助我们处理的是数值型的数据,当然在数据分析中除了数值型的数据还有好多其他类 ...

  5. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍 一、pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主

    利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍 一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目 ...

  6. Python之pandas,series,可视化

    七月在线之python数据处理 python常用导入函数 ndarray np之常用函数创建 ndarray之聚合操作 pandas pandas之series Series的创建 Series的索引 ...

  7. python删除所有core文件_python – 从pandas.core.series.Series中删除前导零

    我有一个带有数据的pandas.core.series.Series 0 [00115840, 00110005, 001000033, 00116000... 1 [00267285, 002636 ...

  8. Python实训day09am【Pandas、Series、DataFrame数据帧】

    Python实训-15天-博客汇总表 目录 1.Pandas 1.1.安装Pandas库 1.2.两种数据对象 2.一列数据Series 2.1.获取Series与数据个数 2.2.切片-loc-il ...

  9. Python数据分析pandas之series初识

    Python数据分析pandas之series初识 声明与简介 pandas是一个基于python的.快速的.高效.灵活.易用的开源的数据处理.分析包(工具)..pandas构建在numpy之上,它通 ...

最新文章

  1. 第一章课后习题(Java)
  2. 使用 Android Studio 进行测试 (二) UI 测试
  3. React Native --网络请求(fetch)
  4. CentOS7.7安装MySQL5.6并配置环境变量(详细版)
  5. dev c++ 代码补全_学习干货——玩转DEV—C++
  6. Spring学习手册 1:Spring MVC 返回JSON数据
  7. Dubbo与Zookeeper伪集群部署
  8. 搜狗输入法电脑版_搜狗输入法上线墨水屏定制版
  9. idea 的精准搜索_intellij idea 的全局搜索快捷键方法
  10. C# Json、datatable、model互相转换
  11. Mootools 1.4 官方网站的API使用说明存在错误
  12. 【电子技术基础(精华版)】整流与滤波电路
  13. idea2020.1.1 窗口显示 Outdated version. 完美解决
  14. windows命令行下schtasks创建定期任务
  15. Dreamweaver自带流体布局+自己添加,后附效果,不知代码有错误没?请行家指正!多谢先
  16. Numpy库的下载及安装(吐血总结)
  17. 【产品设计】瀑布流与分页模式比较
  18. 什么是VOIP和SIP?
  19. FilterConfig接口及其使用方法详解
  20. 怎么把柱形图和折线图放在一起_EXCEL中统计图表怎么合并在一起?(如柱形图和折线图)...

热门文章

  1. 室内施工图LiSP_室内设计施工图各种符号_
  2. QForkMasterInit: system error caught. error code=0x000005af, message=VirtualAllocEx failed.: unknow
  3. Nature综述:Rob Knight等大佬手把手教你开展菌群研究
  4. 优酷在线播放器 html5,看优酷,用HTML5
  5. 稀疏矩阵压缩sparse.csr_matrix函数与sparse.csc_matric详解
  6. 免费开通PTrade与QMT量化交易系统
  7. 使用mkcert工具生成受信任的本地SSL证书
  8. SpringBoot + Spring Security 学习笔记(一)自定义基本使用及个性化登录配置
  9. fedora23_x86_64通过dnf升级到fedora24
  10. 大学计算机基础应用word,大学计算机基础实验4-word文档的综合应用