2017-10-04 47 views
1

我有一個數據框每天的值(見下面的df)。 我想將每週的「預測」字段分組,但將星期一作爲一週的第一天。每週組python熊貓數據框(星期一開始)

目前我可以通過pd.TimeGrouper做到這一點( 'W')(見下文df_final),但該指數本週累計組在週日開始(見下df_final)

import pandas as pd 
data = [("W1","G1",1234,pd.to_datetime("2015-07-1"),8), 
     ("W1","G1",1234,pd.to_datetime("2015-07-30"),2), 
     ("W1","G1",1234,pd.to_datetime("2015-07-15"),2), 
     ("W1","G1",1234,pd.to_datetime("2015-07-2"),4), 
     ("W1","G2",2345,pd.to_datetime("2015-07-5"),5), 
     ("W1","G2",2345,pd.to_datetime("2015-07-7"),1), 
     ("W1","G2",2345,pd.to_datetime("2015-07-9"),1), 
     ("W1","G2",2345,pd.to_datetime("2015-07-11"),3)] 

labels = ["Site","Type","Product","Date","Forecast"] 
df = pd.DataFrame(data,columns=labels).set_index(["Site","Type","Product","Date"]) 
df 


           Forecast 
Site Type Product Date     
W1 G1 1234 2015-07-01   8 
        2015-07-30   2 
        2015-07-15   2 
        2015-07-02   4 
    G2 2345 2015-07-05   5 
        2015-07-07   1 
        2015-07-09   1 
        2015-07-11   3 



df_final = (df 
    .reset_index() 
    .set_index("Date") 
    .groupby(["Site","Product",pd.TimeGrouper('W')])["Forecast"].sum() 
    .astype(int) 
    .reset_index()) 
df_final["DayOfWeek"] = df_final["Date"].dt.dayofweek 
df_final 

    Site Product  Date Forecast DayOfWeek 
0 W1  1234 2015-07-05  12   6 
1 W1  1234 2015-07-19   2   6 
2 W1  1234 2015-08-02   2   6 
3 W1  2345 2015-07-05   5   6 
4 W1  2345 2015-07-12   5   6 
+0

我認爲'W-MON'而不是'W'應該有所幫助。 – jezrael

回答

1

使用W-MON代替W,檢查anchored offsets

df_final = (df 
    .reset_index() 
    .set_index("Date") 
    .groupby(["Site","Product",pd.TimeGrouper('W-MON')])["Forecast"].sum() 
    .astype(int) 
    .reset_index()) 
df_final["DayOfWeek"] = df_final["Date"].dt.dayofweek 
print (df_final) 
    Site Product  Date Forecast DayOfWeek 
0 W1  1234 2015-07-06  12   0 
1 W1  1234 2015-07-20   2   0 
2 W1  1234 2015-08-03   2   0 
3 W1  2345 2015-07-06   5   0 
4 W1  2345 2015-07-13   5   0 
相關問題