2016-01-21 115 views
2

我需要在大熊貓中執行一個複雜的羣,除以日期範圍。羣熊貓除以日期範圍

       PROFIT TOTAL MATCH_HOME PROFIT_2 
LEAGUE MATCH_DATE MATCH_HOME    
    1 2007-06-05 (1.2, 1.6] -20.00 1.00  1.55  21.00 
    2 2007-08-10 (2.4, 2.8] 19.50 0.50  2.50 -19.00 
    5 2012-04-05 (2, 2.4] 18.20 0.20  2.40 -18.00 
     2014-09-05 (2, 2.4] -14.00 1.40  2.20  15.40 

我得到這個數據幀由下面的代碼:

bins = np.linspace(0, 10, 25, endpoint=False) 
g1 = dfml.groupby([dfml.LEAGUE, dfml.MATCH_DATE, 
        pd.cut(dfml.MATCH_HOME, bins)]).agg({'MATCH_HOME' : 'min', 
                 'PROFIT': sum, 
                 'PROFIT_2': sum, 
                 'TOTAL': sum 
                 }) 
g1[g1['TOTAL'] > 0] 

我怎麼能按Month而不是Day

+0

IIUC可以時間值轉換到週期 - 'DF ['MATCH_DATE '] = df ['MATCH_DATE']。dt.to_period('M')' ' – jezrael

回答

2

也許你可以使用dt.month

bins = np.linspace(0, 10, 25, endpoint=False) 
g1 = dfml.groupby([dfml.LEAGUE, dfml.MATCH_DATE.dt.month, 
        pd.cut(dfml.MATCH_HOME, bins)]).agg({'MATCH_HOME' : 'min', 
                 'PROFIT': sum, 
                 'PROFIT_2': sum, 
                 'TOTAL': sum 
                 }) 
g1[g1['TOTAL'] > 0] 

或者如果您需要多年和月在一起,轉換datetimeperiod

df['MATCH_DATE'] = df['MATCH_DATE'].dt.to_period('M')