将平均每五分钟数据作为 pandas 数据帧中的一个数据点 [英] averaging every five minutes data as one datapoint in pandas dataframe

查看:148
本文介绍了将平均每五分钟数据作为 pandas 数据帧中的一个数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的熊猫数据框

  1。 2013-10-09 09:00:05 
2. 2013-10-09 09:01:00
3. 2013-10-09 09:02:00
4. .. ..........
5. ............
6. ............
7. 2013-10-10 09:15:05
8. 2013-10-10 09:16:00
9. 2013-10-10 09:17:00

我想通过平均每5分钟的数据并形成1个数据点来缩小Dataframe的大小。像这样



  1。 2013-10-09 09:05:00 
2. 2013-10-09 09:10:00
3. 2013-10-09 09:15:00

有人可以帮我吗?

解决方案>

您可能需要查看 pandas.resample

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

或作为 how ='mean'是默认参数:

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

例如:

 >> ; 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
数据
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

您可以找到更多例子 here


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.   ............
7. 2013-10-10 09:15:05
8. 2013-10-10 09:16:00 
9. 2013-10-10 09:17:00

I would like reduce the size of the Dataframe by averaging every 5 mins data and forming 1 datapoint for it ..like this

1. 2013-10-09 09:05:00
2. 2013-10-09 09:10:00
3. 2013-10-09 09:15:00

Can someone help me with this ??

解决方案

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.

这篇关于将平均每五分钟数据作为 pandas 数据帧中的一个数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆