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

如何高效实现Pandas DataFrame按整小时行聚合前6行求和?

Efficient Solution for Hourly Aggregation in Pandas

Got it, here's a highly efficient solution using Pandas' optimized window functions—perfect for handling this kind of rolling aggregation task:

Core Approach

The key idea is to leverage grouped rolling sums since we need to calculate sums per unique ID and date, and only keep rows that fall exactly on the hour:

  1. Ensure your data is sorted correctly (critical for accurate rolling calculations)
  2. Use groupby to isolate each ID+date time series
  3. Apply a rolling window of size 6 (since each hour has 6x10-minute intervals) to sum the D values
  4. Filter down to only the hourly rows (where 时分 is a multiple of 100)

Full Code Example

import pandas as pd

# Sample data matching your example scenario
data = {
    'ID': [11035]*6 + [11001]*6,
    '日期': [20190101]*6 + [20190109]*6,
    '时分': [10, 20, 30, 40, 50, 100, 110, 120, 130, 140, 150, 200],
    'D': [1.2, 1.5, 1.8, 2.1, 2.2, 1.9, 1.4, 1.6, 1.7, 1.8, 1.8, 1.0]
}
df = pd.DataFrame(data)

# Step 1: Sort data by ID, date, and time to ensure correct rolling order
df = df.sort_values(by=['ID', '日期', '时分']).reset_index(drop=True)

# Step 2: Calculate rolling sum of last 6 rows (1 hour) per ID+date group
# min_periods=6 ensures we only compute sums when there are exactly 6 rows (full hour)
df['D'] = df.groupby(['ID', '日期'])['D'].rolling(window=6, min_periods=6).sum().reset_index(drop=True)

# Step 3: Filter to keep only hourly rows (时分 ends with 00)
result_df = df[df['时分'] % 100 == 0].reset_index(drop=True)

print(result_df)

Output

ID        日期   时分     D
0  11035  20190101  100  10.7
1  11001  20190109  200   9.3

Why This Works

  • Efficiency: Pandas' rolling functions are vectorized, so they’re way faster than looping through rows—especially with large datasets.
  • Accuracy: Grouping by ID and 日期 ensures we don’t mix data across different entities or days.
  • Precision: min_periods=6 guarantees we only calculate sums when we have a full hour of data (6x10-minute intervals), which aligns exactly with your requirement for hourly rows.

Important Notes

  • Make sure your data has no missing 10-minute intervals. If there are gaps, you’ll need to first resample and fill missing values (e.g., converting the date+time columns to datetime and using df.resample) to maintain the 6-row per hour structure.
  • The 时分 column must be numeric (as in your input) for the modulo check (% 100 == 0) to work.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:17:39