You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Pandas中为指定列计算Z-score并保留所有列的实现问询

时间序列DataFrame的Z-score计算及前瞻问题解决

一、需求实现代码

针对你的DataFrame,为open_int、large_spec_long、large_spec_short三列计算Z-score并新增_z后缀列,同时保留原结构,分两种场景处理:

1. 无前瞻的时间序列场景(推荐,避免未来数据泄露)

如果数据是按时间排序的序列,必须用累积窗口计算,确保每个时刻的Z-score只用到当前及之前的历史数据:

import pandas as pd
import numpy as np

# 先确保数据按date升序排列(关键,避免时间顺序混乱)
df = df.sort_values("date").reset_index(drop=True)

# 指定需要处理的列
target_cols = ["open_int", "large_spec_long", "large_spec_short"]

for col in target_cols:
    # 累积计算到当前行的均值和样本标准差(ddof=1)
    rolling_mean = df[col].expanding().mean()
    rolling_std = df[col].expanding().std(ddof=1)
    # 计算Z-score,处理标准差为0的边界情况
    df[f"{col}_z"] = np.where(rolling_std == 0, 0, (df[col] - rolling_mean) / rolling_std)

2. 全局统计量场景(无时间序列要求)

如果不需要考虑时间顺序(非时序数据),直接用整个数据集的均值和标准差计算:

target_cols = ["open_int", "large_spec_long", "large_spec_short"]

for col in target_cols:
    col_mean = df[col].mean()
    col_std = df[col].std(ddof=1)
    df[f"{col}_z"] = (df[col] - col_mean) / col_std

二、前瞻问题验证方法

前瞻问题本质是用了未来数据计算当前统计量,验证方式如下:

1. 拆分数据集对比

把数据按时间拆分为训练集和测试集,用训练集的统计量计算测试集Z-score,和全局统计量的结果对比:

split_point = int(len(df) * 0.8)
train = df.iloc[:split_point]
test = df.iloc[split_point:].copy()

for col in target_cols:
    # 用训练集统计量计算"无前瞻"的Z-score
    train_mean = train[col].mean()
    train_std = train[col].std(ddof=1)
    test[f"{col}_z_no_lookahead"] = (test[col] - train_mean) / train_std
    
    # 用全局统计量计算"有前瞻"的Z-score
    global_mean = df[col].mean()
    global_std = df[col].std(ddof=1)
    test[f"{col}_z_lookahead"] = (test[col] - global_mean) / global_std

# 查看两列差异:如果数值不同,说明全局计算存在前瞻泄露
print(test[[f"{col}_z_no_lookahead" for col in target_cols] + [f"{col}_z_lookahead" for col in target_cols]])

2. 手动验证累积窗口

取前几行数据,手动计算累积均值和Z-score,和代码输出对比:

# 打印前5行的原始值、累积均值和Z-score
print(df[["date", "open_int", "open_int_z"]].head())
# 比如第3行的累积均值 = (第1行open_int + 第2行open_int + 第3行open_int)/3,和输出的open_int_z对应的均值计算结果一致,说明无前瞻

三、结构保留说明

上述代码均直接在原DataFrame上新增_z后缀列,不会修改date、market_exchange及原始目标列,完全保留你需要的完整结构。

内容的提问来源于stack exchange,提问作者TimExcellent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 00:15:30