自学内容网 自学内容网

详解 Pandas 的窗口函数

Pandas 中常用的窗口函数有滑动窗口 rolling() 和扩张窗口 expanding(),功能主要是对数据进行滚动计算、滑动统计等操作。

一、滑动窗口 rolling

固定窗口大小,进行滑动计算

1. 数据准备

se = pd.Series(data=[100, 110, 120, 130, 110, 140, 150, 80, 110, 130],
               index=["2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-05", "2019-01-06", "2019-01-07",
                      "2019-01-08", "2019-01-09", "2019-01-10"])

print(se)
2019-01-01    100
2019-01-02    110
2019-01-03    120
2019-01-04    130
2019-01-05    110
2019-01-06    140
2019-01-07    150
2019-01-08     80
2019-01-09    110
2019-01-10    130

2. 方法声明

def rolling(
        self,
        window: int | dt.timedelta | str | BaseOffset | BaseIndexer, # 窗口大小
        min_periods: int | None = None, # 每个窗口内最少包含的观测值的数量,如果小于这个值的窗口,则结果为 NA
        center: bool_t = False, # 是否把当前行数据设为窗口居中位置,默认是窗口结束位置
        win_type: str | None = None,
        on: str | None = None, # 指定 DataFrame 中在窗口中观测的列
        axis: Axis | lib.NoDefault = lib.no_default, # 指定按行或按列进行计算
        closed: IntervalClosedType | None = None,
        step: int | None = None, # 步长
        method: str = "single",
    ) -> Window | Rolling

3. 基本使用

# 统计前 3 天的累计值(包含当天)
w = se.rolling(window=3).sum()
print(w)
2019-01-01      NaN
2019-01-02      NaN
2019-01-03    330.0
2019-01-04    360.0
2019-01-05    360.0
2019-01-06    380.0
2019-01-07    400.0
2019-01-08    370.0
2019-01-09    340.0
2019-01-10    320.0
# 统计前3天的累计值(包含当天),不足 3 天按实际统计
w2 = se.rolling(window=3, min_periods=1).sum()

print(w2)
2019-01-01    100.0
2019-01-02    210.0
2019-01-03    330.0
2019-01-04    360.0
2019-01-05    360.0
2019-01-06    380.0
2019-01-07    400.0
2019-01-08    370.0
2019-01-09    340.0
2019-01-10    320.0
# 统计每3天的累计值(当天的前一天、当天、当天的后一天)
w3 = se.rolling(window=3, center=True).sum()
print(w3)
2019-01-01      NaN
2019-01-02    330.0
2019-01-03    360.0
2019-01-04    360.0
2019-01-05    380.0
2019-01-06    400.0
2019-01-07    370.0
2019-01-08    340.0
2019-01-09    320.0
2019-01-10      NaN

二、扩张窗口 expanding

只设置最小的观测值数量,不固定窗口大小,实现累计计算

1. 数据准备

1. 数据准备

se = pd.Series(data=[100, 110, 120, 130, 110, 140, 150, 80, 110, 130],
               index=["2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-05", "2019-01-06", "2019-01-07",
                      "2019-01-08", "2019-01-09", "2019-01-10"])

print(se)
2019-01-01    100
2019-01-02    110
2019-01-03    120
2019-01-04    130
2019-01-05    110
2019-01-06    140
2019-01-07    150
2019-01-08     80
2019-01-09    110
2019-01-10    130

2. 方法声明

def expanding(
        self,
        min_periods: int = 1, # 设置最少包含的观测值的数量,如果小于这个值的窗口,则结果为 NA
        axis: Axis | lib.NoDefault = lib.no_default, # 指定按行或按列进行计算
        method: Literal["single", "table"] = "single",
    ) -> Expanding

3. 基本使用

# 统计累积到当天的总值
w = se.expanding(min_periods=1).sum() # 类似 cumsum() 函数的累计求和
print(w)
2019-01-01     100.0
2019-01-02     210.0
2019-01-03     330.0
2019-01-04     460.0
2019-01-05     570.0
2019-01-06     710.0
2019-01-07     860.0
2019-01-08     940.0
2019-01-09    1050.0
2019-01-10    1180.0
# 统计累积到当天的总值,最少有 2 天
w2 = se.expanding(min_periods=2).sum()
print(w2)
2019-01-01       NaN
2019-01-02     210.0
2019-01-03     330.0
2019-01-04     460.0
2019-01-05     570.0
2019-01-06     710.0
2019-01-07     860.0
2019-01-08     940.0
2019-01-09    1050.0
2019-01-10    1180.0

三、窗口中支持的聚合函数

聚合函数说明
sum()值的总和
count()非空观测值数量
mean()值的平均值
median()值的中位数
min()最小值
max()最大值
std()值的标准差
var()值的方差
skew()样品偏斜度(三阶矩)
kurt()样品峰度(四阶矩)
quantile()样本分位数(百分位上的值)
cov()无偏协方差(二元)
corr()相关性(二进制)
agg()通用聚合
apply()自定义函数

原文地址:https://blog.csdn.net/weixin_44480009/article/details/142421865

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!