15-Python Pandas窗口函数

Python Pandas窗口函数

为了能更好地处理数值型数据,Pandas 提供了几种窗口函数,比如移动函数(rolling)、扩展函数(expanding)和指数加权函数(ewm)。

窗口函数应用场景非常多。举一个简单的例子:现在有 10 天的销售额,而您想每 3 天求一次销售总和,也就说第五天的销售额等于(第三天 + 第四天 + 第五天)的销售额之和,此时窗口函数就派上用场了。

窗口是一种形象化的叫法,这些函数在执行操作时,就如同窗口一样在数据区间上移动。

本节学习主要讲解如何在 DataFrame 和 Series 对象上应用窗口函数。

rolling()

rolling() 又称移动窗口函数,它可以与 mean、count、sum、median、std 等聚合函数一起使用。为了使用方便,Pandas 为移动函数定义了专门的方法聚合方法,比如 rolling_mean()、rolling_count()、rolling_sum() 等。其的语法格式如下:

rolling(window=n, min_periods=None, center=False)

常用参数说明如下:

参数名称说明
window默认值为 1,表示窗口的大小,也就是观测值的数量,
min_periods表示窗口的最小观察值,默认与 window 的参数值相等。
center是否把中间值做为窗口标准,默认值为 False。

下面看一组示例:

import pandas as pd
import numpy as np
#生成时间序列
df = pd.DataFrame(np.random.randn(8, 4),index = pd.date_range('12/1/2020', periods=8),columns = ['A', 'B', 'C', 'D'])
print(df)
#每3个数求求一次均值
print(df.rolling(window=3).mean())

输出结果:

                   A         B         C         D
2020-12-01  1.098587  0.170765  1.855523 -2.356607
2020-12-02  1.228804 -0.008233  0.959217 -0.580855
2020-12-03  0.633766 -0.651728 -0.390314 -1.314763
2020-12-04  0.183158  0.956447  0.104921  1.408480
2020-12-05  0.013486  0.658887  0.263779  0.391889
2020-12-06 -1.237204 -0.103082  1.295277 -0.578087
2020-12-07 -1.445140 -0.909246  0.586728 -1.163454
2020-12-08 -0.042114 -0.354415 -0.303725 -1.149433
                   A         B         C         D
2020-12-01       NaN       NaN       NaN       NaN
2020-12-02       NaN       NaN       NaN       NaN
2020-12-03  0.987052 -0.163065  0.808142 -1.417408
2020-12-04  0.681909  0.098829  0.224608 -0.162379
2020-12-05  0.276803  0.321202 -0.007204  0.161868
2020-12-06 -0.346853  0.504084  0.554659  0.407427
2020-12-07 -0.889619 -0.117814  0.715262 -0.449884
2020-12-08 -0.908152 -0.455581  0.526093 -0.963658

window=3表示是每一列中依次紧邻的每 3 个数求一次均值。当不满足 3 个数时,所求值均为 NaN 值,因此前两列的值为 NaN,直到第三行值才满足要求 window =3。求均值的公式如下所示:

(index1+index2+index3)/3

举个好计算的例子:

import pandas as pd
import numpy as np
#生成时间序列
df = pd.DataFrame(np.arange(32).reshape(8,4),index = pd.date_range('12/1/2020', periods=8),columns = ['A', 'B', 'C', 'D'])
print(df)
#每3个数求求一次均值
print(df.rolling(window=3).mean())


运行结果:

             A   B   C   D
2020-12-01   0   1   2   3
2020-12-02   4   5   6   7
2020-12-03   8   9  10  11
2020-12-04  12  13  14  15
2020-12-05  16  17  18  19
2020-12-06  20  21  22  23
2020-12-07  24  25  26  27
2020-12-08  28  29  30  31
               A     B     C     D
2020-12-01   NaN   NaN   NaN   NaN
2020-12-02   NaN   NaN   NaN   NaN
2020-12-03   4.0   5.0   6.0   7.0
2020-12-04   8.0   9.0  10.0  11.0
2020-12-05  12.0  13.0  14.0  15.0
2020-12-06  16.0  17.0  18.0  19.0
2020-12-07  20.0  21.0  22.0  23.0
2020-12-08  24.0  25.0  26.0  27.0

expanding()

expanding() 又叫扩展窗口函数,扩展是指由序列的第一个元素开始,逐个向后计算元素的聚合值。

下面示例,min_periods = n表示向后移动 n 个值计求一次平均值:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4),
      index = pd.date_range('1/1/2018', periods=10),
      columns = ['A', 'B', 'C', 'D'])
print (df.expanding(min_periods=3).mean())

输出结果:

                   A         B         C         D
2020-01-01       NaN       NaN       NaN       NaN
2020-01-02       NaN       NaN       NaN       NaN
2020-01-03 -0.567833  0.258723  0.498782  0.403639
2020-01-04 -0.384198 -0.093490  0.456058  0.459122
2020-01-05 -0.193821  0.085318  0.389533  0.552429
2020-01-06 -0.113941  0.252397  0.214789  0.455281
2020-01-07  0.147863  0.400141 -0.062493  0.565990
2020-01-08 -0.036038  0.452132 -0.091939  0.371364
2020-01-09 -0.043203  0.368912 -0.033141  0.328143
2020-01-10 -0.100571  0.349378 -0.078225  0.225649

设置 min_periods=3,表示至少 3 个数求一次均值,计算方式为 (index0+index1+index2)/3,而 index3 的计算方式是 (index0+index1+index2+index3)/3,依次类推。

ewm()

ewm(全称 Exponentially Weighted Moving)表示指数加权移动。ewn() 函数先会对序列元素做指数加权运算,其次计算加权后的均值。该函数通过指定 com、span 或者 halflife 参数来实现指数加权移动。示例如下:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4),
   index = pd.date_range('12/1/2020', periods=10),
   columns = ['A', 'B', 'C', 'D'])
#设置com=0.5,先加权再求均值
print(df.ewm(com=0.5).mean())

输出结果:

                   A         B         C         D
2020-12-01 -1.076618  0.519650 -1.538041  1.063112
2020-12-02  0.366685 -0.908325  0.072165  0.398068
2020-12-03  0.100632 -0.729668  0.836416  0.282776
2020-12-04 -0.535957  0.456067 -0.410885 -0.984390
2020-12-05 -0.768264 -0.264365  0.293843  1.128267
2020-12-06  0.246798  0.947890  0.525974 -0.504689
2020-12-07  0.188330  0.748632  1.123739 -0.511085
2020-12-08  0.960215 -0.415316 -0.530754 -0.353660
2020-12-09  0.148049  0.434885 -0.025062  0.508717
2020-12-10 -0.159873 -0.893985  0.480894 -0.867417

在数据分析的过程中,使用窗口函数能够提升数据的准确性,并且使数据曲线的变化趋势更加平滑,从而让数据分析变得更加准确、可靠。

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值