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

如何用Pandas GroupBy结合时间区间过滤计算学生科目分数变化?

按指定时间区间计算学生科目分数变化的高效方案

核心思路

用Pandas的groupby结合自定义函数替代多层for循环:先按Student和Subject分组,对每个分组内的数据按时间区间过滤,再计算分数差值,全程复用Pandas的向量化操作提升效率。

步骤实现

1. 修正时间区间定义

先确保时间区间的日期格式正确(原代码中pd.to_datetime(2021-01-01)是错误的,会被解析为数字运算结果,需传入字符串):

import pandas as pd

# 定义目标时间区间列表
period_time = [
    (pd.to_datetime('2021-01-01'), pd.to_datetime('2021-02-10')),
    (pd.to_datetime('2021-02-14'), pd.to_datetime('2021-02-17'))
]

2. 定义分组处理函数

编写函数处理每个Student-Subject分组,完成时间过滤、分数差计算:

def process_group(group):
    # 先按时间排序,保证分数差计算的顺序正确
    sorted_group = group.sort_values('Datetime').reset_index(drop=True)
    period_results = []
    
    for start, end in period_time:
        # 筛选当前时间区间内的数据
        time_mask = (sorted_group['Datetime'] >= start) & (sorted_group['Datetime'] <= end)
        filtered_data = sorted_group[time_mask].copy()
        
        # 计算分数变化(首次记录为NaN)
        filtered_data['Score_Diff'] = filtered_data['Score'].diff()
        # 标记所属时间区间,便于后续区分
        filtered_data['Time_Period'] = f"{start.date()} ~ {end.date()}"
        
        period_results.append(filtered_data)
    
    # 合并当前分组所有时间区间的结果
    return pd.concat(period_results, ignore_index=True)

3. 应用分组计算

通过groupby.apply()将函数应用到每个分组,得到最终结果:

# 按Student和Subject分组,执行处理逻辑
final_output = df.groupby(['Student', 'Subject'], group_keys=False).apply(process_group)

关键注意事项

  • 排序必要性:必须对每个分组内的Datetime排序,否则diff()会基于原始数据的无序状态计算,结果完全错误。
  • 性能优势:groupby底层基于Pandas的向量化实现,比Python多层for循环效率提升显著,数据量越大优势越明显。
  • 时间格式校验:确保原始数据的Datetime字段是datetime64类型,若不是可先通过pd.to_datetime(df['Datetime'])转换。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 14:15:41