2016-04-03 85 views
3

的Python使用大熊貓按天數來獲得數據的累積和

我已經得到了從Wunderground122天不具有相等的時間採樣間隔的天氣數據。以下是我的數據示例:

Bangor Weather Data from Wunderground 
Datetime,Temp(F),Precip(in.),Snow (in.),PET(in./day),Baro(mBar) 
    2015-12-02 01:30:00,1.1,0.3,0.0,0.45524647117649564,1017.5 
    2015-12-02 01:53:00,1.1,0.3,0.0,0.45524647117649564,1017.6 
    2015-12-02 02:20:00,1.1,0.3,0.0,0.45524647117649564,1017.2 
    2015-12-02 02:53:00,1.7,0.5,0.0,0.500024812603692,1016.7 
    2015-12-02 02:55:00,1.7,0.3,0.0,0.500024812603692,1016.5 
    2015-12-02 03:09:00,1.1,0.3,0.0,0.45524647117649564,1016.1 
    2015-12-02 03:33:00,1.1,0.5,0.0,0.45524647117649564,1016.1 
    2015-12-02 03:53:00,1.7,0.8,0.0,0.500024812603692,1016.1 
    2015-12-02 04:34:00,1.7,0.5,0.0,0.500024812603692,1015.1 
    2015-12-02 04:46:00,1.7,0.5,0.0,0.500024812603692,1015.1 
    2015-12-02 04:53:00,1.7,0.8,0.0,0.500024812603692,1015.1 
    2015-12-02 05:13:00,1.7,0.0,0.0,0.500024812603692,1014.4 

我想要獲取整個數據集的每日總積雪(重置爲零)。我想我的輸出看起來像:

2015-12-01,0.0 
    2015-12-02,0.0 
    2015-12-03,1.0 
    2015-12-04,3.0 
    2015-12-05,0.0 
    2015-12-06,1.0 

我該如何使用熊貓來做到這一點?

+0

[相關問題](https://stackoverflow.com/questions/14673394/python-pandas-extract-unique-dates-from-time-series)關於從時間序列中提取獨特日期。我懷疑你可以''分組''日期',然後申請'cumsum'。 – Paul

+2

實際上,這有點不清楚 - 你是否希望累積金額在每一天都「重置」爲0,或者你想每天有1個數據點,那天的數值對應於雪的總數積累到那一點?我認爲在這裏展示一個示例輸出會有所幫助。 – Paul

+0

是的,抱歉,缺乏清晰度。我想要特定日子的積雪。所以是的,當有新的一天時,它會重置爲零。 – Strak

回答

2

這就是你想要的嗎?

df.groupby(df.Datetime.dt.date)['Snow (in.)'].sum() 

這會給你每天雪(總和)的量

+2

我想他們想要['cumsum'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.cumsum.html),而不是'sum'。 – Paul

+0

@Paul,IMO'cumsum()'在這種情況下對於積雪來說沒有多大意義 – MaxU

+1

它可能是累積的每日積雪以及... –

0

您還可以使用:

df['Snow (in.)'].resample('D').sum()