0
我在三列上運行簡單的平均操作。我正在將月度數據轉化爲季度平均值。數據是這樣的:內核死亡運行使用Python的簡單平均操作
2000.1 2000.2 2000.3....
18 15 27
我想把它改造成
2000.q1
20
這是我到目前爲止有:
高清convert_housing_data_to_quarters(): '''住房轉換數據轉換爲宿舍並將其作爲數據幀中的平均值 返回。該數據幀應該是包含2000q1到2016q3的 列的數據幀,並應具有形式爲[「State」,「RegionName」]的多索引 。
Note: Quarters are defined in the assignment description, they are
not arbitrary three month periods.
The resulting dataframe should have 67 columns, and 10,730 rows.
'''
# read in the zillow housing data
zillow_df = pd.read_csv('City_Zhvi_AllHomes.csv')
print(zillow_df.iloc[1,1])
print(len(zillow_df))
# slice from 2000q1 to 2016q3
print(zillow_df.columns)
print(zillow_df.columns[6:51])
zillow_df.drop(zillow_df.columns[6:51],axis=1,inplace=True)
# generate quarterly average
y = 2000
q = 1
for i in range(67):
y_q = str(y)+'q'+str(q)
#print(y_q)
print(zillow_df.columns[6+(i)*3])
print(zillow_df[zillow_df.columns[6+(i)*3]])
zillow_df[y_q]=(zillow_df[zillow_df.columns[6+(i)*3]]+zillow_df[zillow_df.columns[6+1+(i)*3]]+zillow_df[zillow_df.columns[6+2+(i)*3]])/3
q=q+1
if q==5:
q=1
y=y+1
return zillow_df.head()
我認爲我的代碼是正確的,但每次我在ipython筆記本上運行它。它說內核死了。我不知道爲什麼。
你能解釋一下爲什麼這種變化是必要的,是什麼原因導致內核死。 –
困難的問題,但最簡單的答案是在熊貓中使用循環沒有最優化,而且非常慢。那麼可能會耗盡內存?也許一些有問題的數據?也許更大的數據?但最好的是使用一些矢量化的解決方案,比如'resample',我認爲它在這裏完美運行。 – jezrael