答案 0 :(得分:0)
如果您的问题在没有数据的情况下不明确,请考虑将代码调整为pandas数据操作方法,例如条件.loc
,groupby
或pivot_table
,而不是循环遍历所有行并分配值通过索引和手动创建月总和。实际上,通过调整后的方法,您可以查看数据的范围,如果数据在3月之后中断,则无法显示。
...
import calendar
data = pd.read_csv(r'C:\Users\mparlo\Documents\Python\NEWYORK - NEWYORK.csv', header=None)\
.set_axis(["Month", "Day", "Year", "Temperature"], axis=1, inplace=False)
t_bp = 65
data.loc[data['Temperature'] > t_bp, 'hdd'] = 0
data.loc[data['Temperature'] <= t_bp, 'hdd'] = t_bp - data['Temperature']
data.loc[data['Temperature'] > t_bp, 'cdd'] = data['Temperature'] - t_bp
data.loc[data['Temperature'] <= t_bp, 'cdd'] = 0
data['Location'] = 'New York'
data['MonthAbbrev'] = data['Month'].apply(lambda x: calendar.month_abbr[x])
# LONG FORMAT
agg_data = data.group_by(['MonthAbbrev']).agg({'hhd':'sum', 'cdd':'sum'})
# WIDE FORMAT
agg_data = data.pivot_table(index=['Location'], values=['hdd', 'cdd'],
columns='MonthAbbrev', aggfunc='sum')