Pandas: resample timeseries with groupby

后端 未结 4 759
时光说笑
时光说笑 2020-11-27 12:29

Given the below pandas DataFrame:

In [115]: times = pd.to_datetime(pd.Series([\'2014-08-25 21:00:00\',\'2014-08-25 21:04:00\',
                                       


        
4条回答
  •  -上瘾入骨i
    2020-11-27 12:45

    Pandas 0.21 answer: TimeGrouper is getting deprecated

    There are two options for doing this. They actually can give different results based on your data. The first option groups by Location and within Location groups by hour. The second option groups by Location and hour at the same time.

    Option 1: Use groupby + resample

    grouped = df.groupby('Location').resample('H')['Event'].count()
    

    Option 2: Group both the location and DatetimeIndex together with groupby(pd.Grouper)

    grouped = df.groupby(['Location', pd.Grouper(freq='H')])['Event'].count()
    

    They both will result in the following:

    Location                     
    HK        2014-08-25 21:00:00    1
    LDN       2014-08-25 21:00:00    1
              2014-08-25 22:00:00    2
    Name: Event, dtype: int64
    

    And then reshape:

    grouped.unstack('Location', fill_value=0)
    

    Will output

    Location             HK  LDN
    2014-08-25 21:00:00   1    1
    2014-08-25 22:00:00   0    2
    

提交回复
热议问题