averaging every five minutes data as one datapoint in pandas dataframe

前端 未结 1 461
傲寒
傲寒 2021-01-06 09:06

I have a Dataframe in Pandas like this

1. 2013-10-09 09:00:05
2. 2013-10-09 09:01:00
3. 2013-10-09 09:02:00
4.  ............
5.   ............
6.   .........         


        
1条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 09:13

    you may want to look at pandas.resample:

    df['Data'].resample('5Min', how='mean')
    

    or, as how = 'mean' is default parameter:

    df['Data'].resample('5Min')
    

    For example:

    >>> rng = pd.date_range('1/1/2012', periods=10, freq='Min')
    >>> df = pd.DataFrame({'Data':np.random.randint(0, 500, len(rng))}, index=rng)
    >>> df
                         Data
    2012-01-01 00:00:00   488
    2012-01-01 00:01:00   172
    2012-01-01 00:02:00   276
    2012-01-01 00:03:00     5
    2012-01-01 00:04:00   233
    2012-01-01 00:05:00   266
    2012-01-01 00:06:00   103
    2012-01-01 00:07:00    40
    2012-01-01 00:08:00   274
    2012-01-01 00:09:00   494
    >>>
    >>> df['Data'].resample('5Min')
    2012-01-01 00:00:00    234.8
    2012-01-01 00:05:00    235.4
    

    You can find more examples here.

    0 讨论(0)
提交回复
热议问题