计算熊猫数据帧中不同事件的每小时和2小时移动平均值

时间:2017-02-10 06:07:35

标签: python pandas dataframe statistics

我有一个如下所示的数据框:

In the top left, click Add New.

Select Add a Place.

Select the Satellite view and zoom in to the appropriate location.

Drop the marker on top of the building where your place exists.

From the drop-down menu, select a category. 

You can also search for a category.

Enter the name of the place. 

To add multiple names, click Add more names. 

To add more details, click Continue. To discard your edit, click Cancel.

Click Save.

每5分钟获得一次值。 我在数据框中有很多这样的事件。

我希望在新数据框中为每个事件提供1小时2小时的移动平均值。

我怎么可能这样做?

2 个答案:

答案 0 :(得分:1)

您可以使用适当的窗口使用pandas rolling_mean函数。如果每5分钟有一个值,要计算1小时窗口的移动平均值,您将传递的参数窗口为12,而在2小时的情况下为24。例如,检查一下 Moving Average- Pandas

答案 1 :(得分:1)

pandas 0.19

d1 = df.set_index('DateTime').sort_index()
ma_1h = d1.groupby('Event').rolling('H').mean()
ma_2h = d1.groupby('Event').rolling('2H').mean()

ma_1h.head()

enter image description here

ma_2h.head()

enter image description here

pandas< 0.19
由于时间戳是固定间隔的,因此我们可以获取精确的行数

ma_1h = d1.groupby('Event').rolling(12).mean().dropna()
ma_2h = d1.groupby('Event').rolling(24).mean().dropna()