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

pandas按CSV的Month列分组计算平均温度输出全量数据如何解决

pandas分组计算月均温度问题排查

错误点

  • mean是pandas的聚合计算方法,未加()调用仅会返回方法对象本身,不会执行实际的平均值计算
  • 未保存分组计算的结果,直接打印df.groupby输出的是DataFrame的groupby方法对象,而非计算得到的月均温度数据

修正代码

import pandas as pd
import matplotlib.pyplot as plt
    
df = pd.read_csv('louisville_weather_data.csv', usecols=['Temperature', 'Wind Speed', 'Precipitation', 'Day', 'Month'])
# 按月份分组计算温度平均值并保存结果
monthly_avg_temp = df.groupby(by='Month')['Temperature'].mean()
# 输出分组计算结果
print(monthly_avg_temp)

# 补充后续绘图逻辑示例
monthly_avg_temp.plot(kind='bar', xlabel='月份', ylabel='平均温度', title='路易斯维尔月均温度统计图')
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 00:36:03