5

我有一个带有事件的 DataFrame。一个或多个事件可以在某个日期发生(因此该日期不能是索引)。日期范围是几年。我想按年和月分组并计算类别值。谢谢

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

我试过了:

df.groupby(df['DateOccurence'])
4

2 回答 2

7

对于月份和年份的突破,我经常在数据框中添加额外的列,将日期分解为每个部分:

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]

它增加了空间复杂度(向 df 添加列),但时间复杂度较低(对 groupby 的处理较少)比 datetime 索引要少,但这完全取决于您。日期时间索引是熊猫做事的更多方式。

按年、月、日突破后,您可以进行任何您需要的分组。

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

要获得跨越多年的月份:

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

或者在 Andy Hayden 的日期时间索引中

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

您可以简单地选择哪种方法更适合您的需求。

于 2013-10-18T03:04:38.407 回答
5

您可以将value_counts应用于 SeriesGroupby (对于列):

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

我实际上希望这能返回以下DataFrame,但您需要将其拆开:

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

如果有多个具有相同日期的不同类别,它们将位于同一行...

于 2013-10-17T20:41:09.083 回答