2013-10-17 26 views
5

Possiedo un DataFrame con eventi. Uno o più eventi possono verificarsi in una data (quindi la data non può essere un indice). L'intervallo di date è di diversi anni. Voglio raggruppare anni e mesi e avere un conteggio dei valori della categoria. ThnxPandas groupby date

in [12]: df = pd.read_excel('Pandas_Test.xls', 'sheet1') 
In [13]: df 
Out[13]: 
    EventRefNr  DateOccurence  Type Category 
0  86596 2010-01-02 00:00:00  3 Small 
1  86779 2010-01-09 00:00:00 13 Medium 
2  86780 2010-02-10 00:00:00  6 Small 
3  86781 2010-02-09 00:00:00 17 Small 
4  86898 2010-02-10 00:00:00  6 Small 
5  86898 2010-02-11 00:00:00  6 Small 
6  86902 2010-02-17 00:00:00  9 Small 
7  86908 2010-02-19 00:00:00  3 Medium 
8  86908 2010-03-05 00:00:00  3 Medium 
9  86909 2010-03-06 00:00:00  8 Small 
10  86930 2010-03-12 00:00:00 29 Small 
11  86934 2010-03-16 00:00:00  9 Small 
12  86940 2010-04-08 00:00:00  9  High 
13  86941 2010-04-09 00:00:00 17 Small 
14  86946 2010-04-14 00:00:00 10 Small 
15  86950 2011-01-19 00:00:00 12 Small 
16  86956 2011-01-24 00:00:00 13 Small 
17  86959 2011-01-27 00:00:00 17 Small 

ho provato:

df.groupby(df['DateOccurence']) 
+0

puoi mostrare il codice che hai provato? – Jeff

risposta

4

È possibile applicare value_counts al SeriesGroupby (per la colonna):

In [11]: g = df.groupby('DateOccurence') 

In [12]: g.Category.apply(pd.value_counts) 
Out[12]: 
DateOccurence   
2010-01-02  Small  1 
2010-01-09  Medium 1 
2010-02-09  Small  1 
2010-02-10  Small  2 
2010-02-11  Small  1 
2010-02-17  Small  1 
2010-02-19  Medium 1 
2010-03-05  Medium 1 
2010-03-06  Small  1 
2010-03-12  Small  1 
2010-03-16  Small  1 
2010-04-08  High  1 
2010-04-09  Small  1 
2010-04-14  Small  1 
2011-01-19  Small  1 
2011-01-24  Small  1 
2011-01-27  Small  1 
dtype: int64 

realtà ho sperato questo per restituire il seguente dataframe, ma è necessario unstack it:

In [13]: g.Category.apply(pd.value_counts).unstack(-1).fillna(0) 
Out[13]: 
       High Medium Small 
DateOccurence      
2010-01-02  0  0  1 
2010-01-09  0  1  0 
2010-02-09  0  0  1 
2010-02-10  0  0  2 
2010-02-11  0  0  1 
2010-02-17  0  0  1 
2010-02-19  0  1  0 
2010-03-05  0  1  0 
2010-03-06  0  0  1 
2010-03-12  0  0  1 
2010-03-16  0  0  1 
2010-04-08  1  0  0 
2010-04-09  0  0  1 
2010-04-14  0  0  1 
2011-01-19  0  0  1 
2011-01-24  0  0  1 
2011-01-27  0  0  1 

Se non ci fossero più diverse categorie con la stessa data sarebbero sulla stessa riga ...

+0

Grande, e ora come raggruppare per mese? – ArtDijk

+0

@ArtDijk Penso che il trucco qui atm sia usare DatetimeIndex, 'di = pd.DatetimeIndex (df.DateOccurence); g = df.groupby ([di.month, di.year]) ' –

6

Per il mese e l'anno di break out ho spesso aggiungono ulteriori colonne alla struttura dati che scoppiano le date in ogni pezzo:

df['year'] = [t.year for t in df.DateOccurence] 
df['month'] = [t.month for t in df.DateOccurence] 
df['day'] = [t.day for t in df.DateOccurence] 

aggiunge complessità spaziale (l'aggiunta di colonne alla df), ma è meno complessa di tempo (meno di elaborazione su groupby) di un indice di datetime ma è davvero a te. indice datetime è il modo più panda di fare le cose.

Dopo l'apertura per anno, mese, giorno è possibile eseguire qualsiasi gruppo di cui si ha bisogno.

df.groupby['year','month'].Category.apply(pd.value_counts) 

Per ottenere mesi su più anni:

df.groupby['month'].Category.apply(pd.value_counts) 

o in indice di datetime di Andy Hayden

df.groupby[di.month].Category.apply(pd.value_counts) 

si può semplicemente scegliere quale metodo si adatta meglio alle proprie necessità.

Problemi correlati