简体   繁体   English

熊猫每整整一个季度每分钟可获得最小值,最大值,平均值

[英]Pandas get min, max, mean value for every quarter of an hour for a whole week

I've got a Pandas DataFrame with an value for every quarter of an hour for a whole year. 我有一个Pandas DataFrame,它的值是整个一年中每25分钟一次。

Datum
2017-01-01 00:15:00    223.1500
2017-01-01 00:30:00    224.8000
2017-01-01 00:45:00    229.3500
2017-01-01 01:00:00    226.7500
2017-01-01 01:15:00    221.9500
2017-01-01 01:30:00    225.7500
2017-01-01 01:45:00    230.7000

Now I want to plot the min, mean and max value of every quarter of the hour for the average of all weeks. 现在,我要绘制所有周平均值的每季度的最小值,平均值和最大值。

So in the end I should have a DataFrame with 672 rows (Mon 00:00, Mon 00:15, Mon 00:30, ... ,Sun 23:30, Sun 23:45) with min, max, mean . 所以最后我应该有一个具有672行的数据帧(星期一00:00,星期一00:15,星期一00:30,...,星期日23:30,星期日23:45),并带有min,max,mean。

I tried with df.groupby() and also resample() without success. 我试着用df.groupby()resample()没有成功。

You could accomplish that with groupby() and agg() : 您可以使用groupby()agg()

Some example data: 一些示例数据:

import pandas as pd
import numpy as np

np.random.seed(444)

idx = pd.date_range('2017', end='2018', freq='15min')[:-1]
df = pd.DataFrame(np.random.randint(2000, 3000, size=idx.size) / 10,
                  index=idx, columns=['data'])

And the operation you're looking for: 而您正在寻找的操作:

to_grp = [df.index.weekday_name, df.index.time]
grp = df.groupby(to_grp, squeeze=True)['data'].agg(['min', 'mean', 'max'])

Here's a snippet: 这是一个片段:

>>> grp.head()

                   min        mean    max
Friday 00:00:00  200.5  255.253846  299.7
       00:15:00  200.2  250.359615  299.9
       00:30:00  204.0  248.376923  299.4
       00:45:00  203.9  258.228846  299.9
       01:00:00  200.0  252.519231  298.6

>>> grp.shape
(672, 3)

I had thought you could use pd.Grouper(freq='15min') in place of df.index.time , but that seems to be giving some trouble here. 我以为您可以使用pd.Grouper(freq='15min')代替df.index.time ,但这似乎在给您带来一些麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM