TypeError:ufunc&#39; add&#39;没有包含签名匹配类型的循环dtype(&#39; <u32')dtype('<u32')=“”dtype('<u32')=“”

时间:2018-02-14 16:48:55

标签: python pandas dataframe

1 个答案:

答案 0 :(得分:0)

如果您的问题在没有数据的情况下不明确,请考虑将代码调整为pandas数据操作方法,例如条件.locgroupbypivot_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')