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

如何用Pandas.resample实现日均统计及温湿度日极值计算

问题

我有一份间隔5分钟、时长2年的气象站数据DataFrame(展示前30行),需要将其重采样为日粒度,计算各列的日均值,同时获取hum和temp列的日最小值与最大值。我已能通过以下代码实现日均重采样:

davis['datetime']=pd.to_datetime(davis['datetime']) #convert datetime str to datetime
davis.index=davis['datetime']#set index to datetime
davis.drop(columns=['datetime'], inplace=True#remove 'datetime' column
davisdaily=davis.resample('1d', offset='1d').mean()#resample and calculate daily mean

但不清楚如何获取每日的最小值和最大值。

数据示例如下:

hum  wind_speed_avg_last_10_min  rainfall_daily_mm  temp
datetime                                                                      
2021-06-17 15:30:42  67.2                        7.37                0.0  64.2
2021-06-17 15:35:42  67.5                        6.68                0.0  64.8
2021-06-17 15:40:42  65.5                        7.31                0.0  64.6
2021-06-17 15:45:42  65.5                        7.87                0.0  64.7
2021-06-17 15:50:42  62.6                        8.12                0.0  64.6
2021-06-17 15:55:42  63.9                        7.50                0.0  64.9
2021-06-17 16:00:42  62.5                        6.06                0.0  64.6
2021-06-17 16:05:42  61.7                        6.93                0.0  64.6
2021-06-17 16:10:42  60.5                        6.81                0.0  64.9
2021-06-17 16:15:42  61.2                        6.56                0.0  64.9
2021-06-17 16:20:42  62.1                        6.25                0.0  65.1
2021-06-17 16:25:42  63.5                        6.50                0.0  65.1
2021-06-17 16:30:42  59.4                        6.68                0.0  65.0
2021-06-17 16:35:42  58.6                        8.06                0.0  64.9
2021-06-17 16:40:42  58.8                        8.18                0.0  65.0
2021-06-17 16:45:42  59.6                        8.06                0.0  64.9
2021-06-17 16:50:42  59.9                        7.31                0.0  64.9
2021-06-17 16:55:42  57.5                        7.93                0.0  64.7
2021-06-17 17:00:42  57.4                        7.56                0.0  64.3
2021-06-17 17:05:42  57.3                        6.81                0.0  64.5
2021-06-17 17:10:42  57.2                        8.00                0.0  63.8
2021-06-17 17:15:42  56.5                        7.93                0.0  64.2
2021-06-17 17:20:42  54.8                        7.75                0.0  63.8
2021-06-17 17:25:42  57.1                        8.81                0.0  63.6
2021-06-17 17:30:42  58.1                        8.81                0.0  63.4
2021-06-17 17:35:42  56.2                        8.12                0.0  63.1
2021-06-17 17:40:42  55.6                        7.87                0.0  63.0
2021-06-17 17:45:42  55.6                        8.37                0.0  62.7
2021-06-17 17:50:42  56.7                        8.43                0.0  62.8
2021-06-17 17:55:42  55.3                        8.37                0.0  62.7

解决方案

首先修正你原代码中的语法错误(drop行末尾缺少闭合括号),然后可以利用resample配合agg方法,一次性完成所有统计量的计算,有两种常用方式:

方式一:自定义列名(清晰直观)

这种方式可以给每个统计结果指定明确的列名,避免混淆:

# 修正后的时间格式处理
davis['datetime'] = pd.to_datetime(davis['datetime'])
davis.index = davis['datetime']
davis.drop(columns=['datetime'], inplace=True)

# 重采样并计算多维度统计量
davisdaily = davis.resample('1d', offset='1d').agg(
    # 所有列的日均值
    hum_mean=('hum', 'mean'),
    wind_speed_mean=('wind_speed_avg_last_10_min', 'mean'),
    rainfall_mean=('rainfall_daily_mm', 'mean'),
    temp_mean=('temp', 'mean'),
    # hum列的日最小、最大值
    hum_min=('hum', 'min'),
    hum_max=('hum', 'max'),
    # temp列的日最小、最大值
    temp_min=('temp', 'min'),
    temp_max=('temp', 'max')
)

方式二:批量指定聚合函数(简洁高效)

如果想更简洁地对列分组设置统计规则,可以用字典格式传递参数,这种方式会生成多级列名,之后可以扁平化处理:

# 重采样计算
davisdaily = davis.resample('1d', offset='1d').agg(
    {
        # 单统计量列
        'wind_speed_avg_last_10_min': 'mean',
        'rainfall_daily_mm': 'mean',
        # 多统计量列
        'hum': ['mean', 'min', 'max'],
        'temp': ['mean', 'min', 'max']
    }
)

# 扁平化多级列名(可选)
davisdaily.columns = ['_'.join(col).strip() for col in davisdaily.columns.values]

两种方式都能完成需求,可根据个人习惯选择。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 16:37:21