2016-09-20 134 views
3

我有我與讀取的數據與此類似大熊貓平均每天,pandas.resample

Date,Temp1,Temp2 

23-Oct-09 01:00:00,21.1,22.3 

23-Oct-09 04:00:00,22.3,23.8 

23-Oct-09 07:00:00,21.4,21.3 

23-Oct-09 10:00:00,21.5,21.6 

23-Oct-09 13:00:00,22.3,23.8 

23-Oct-09 16:00:00,21.4,21.3 

23-Oct-09 19:00:00,21.1,22.3 

23-Oct-09 22:00:00,21.4,21.3 

24-Oct-09 01:00:00,22.3,23.8 

24-Oct-09 04:00:00,22.3,23.8 

24-Oct-09 07:00:00,21.1,22.3 

24-Oct-09 10:00:00,22.3,23.8 

24-Oct-09 13:00:00,21.1,22.3 

24-Oct-09 16:00:00,22.3,23.8 

24-Oct-09 19:00:00,21.1,22.3 

24-Oct-09 22:00:00,22.3,23.8 

csv文件:

df=pd.read_csv('data.csv', index_col=0) 

並轉換索引日期時間

df.index=pd.to_datetime(df.index) 

現在我想要採取每個日常溫度的意思,我一直在嘗試使用pd.resample如下,但一直在接收錯誤。我讀過關於這裏的pandas.resample文檔和許多例子和我仍處於虧損狀態...

df_avg = df.resample('D', how = 'mean') 

DataError: No numeric types to aggregate

我想df_avg是有日期時間指數和兩個2列的數據幀。我使用熊貓0.17.1和python 3.5.2,非常感謝任何幫助!

回答

2

你需要轉換stringfloat第一:

#add parameter parse_dates for convert to datetime first column 
df=pd.read_csv('data.csv', index_col=0, parse_dates=[0]) 

df['Temp1'] = df.Temp1.astype(float) 
df['Temp2'] = df.Temp2.astype(float) 

df_avg = df.resample('D', how = 'mean') 

如果astype回報error,問題是有一些非數值。所以你需要使用to_numericerrors='coerce' - 那麼所有的「問題」值轉換爲NaN

df['Temp1'] = pd.to_numeric(df.Temp1, errors='coerce') 
df['Temp2'] = pd.to_numeric(df.Temp2, errors='coerce') 

您還可以查看所有的行有問題的值與boolean indexing

print df[pd.to_numeric(df.Temp1, errors='coerce').isnull()] 
print df[pd.to_numeric(df.Temp2, errors='coerce').isnull()] 
+0

如果我的回答對您有所幫助,不要忘記[接受](http://meta.stackexchange.com/a/5235/295067)它。謝謝。 – jezrael

+0

請注意,此方法已棄用,重新採樣代碼現在應爲df.resample('D')。mean() – PJW