按时间分组和其他在 pandas 中的专栏 [英] group by time and other column in pandas

查看:56
本文介绍了按时间分组和其他在 pandas 中的专栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大熊猫数据框,其中包含时间戳记,名称和值列

I have a large pandas dataframe containing columns timestamp, name, and value

index    timestamp                     name   value
0        1999-12-31 23:59:59.000107    A      16
1        1999-12-31 23:59:59.000385    B      12
2        1999-12-31 23:59:59.000404    C      25 
3        1999-12-31 23:59:59.000704    B      15
4        1999-12-31 23:59:59.001281    A      300
5        1999-12-31 23:59:59.002211    C      20
6        1999-12-31 23:59:59.002367    C      3

我想按时间段(例如20ms或20分钟)和名称分组,然后计算每组的平均值.

I want to group by time buckets (say 20ms or 20 minutes) and name, and calculate the average value for each group.

最有效的方法是什么?

推荐答案

您可以使用pd.Grouper,但是它要求您在索引上带有时间戳.因此,您可以尝试执行以下操作:

You can use pd.Grouper, but it requires you to have the timestamps on the index. So you could try something like:

df.set_index('timestamp').groupby([pd.Grouper(freq='20Min'), 'name']).mean()

这篇关于按时间分组和其他在 pandas 中的专栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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