Pandas DataFrame按时间戳分组

问题描述

groupby 通过 pd.Grouper

# optionally, if needed
# df['Timestamp'] = pd.to_datetime(df['Timestamp'], errors='coerce')  
df.groupby(pd.Grouper(key='Timestamp', freq='30min')).count()

resample

df.set_index('Timestamp').resample('30min').count()

解决方法

我有一个用例,其中:

数据的格式为:Col1,Col2,Col3和时间戳。

现在,我只想获取行数与时间戳箱的数量。

也就是说,对于每半小时的存储桶(甚至没有对应行的存储桶),我需要计算有多少行。

时间戳记分布在一年内,因此我无法将其划分为24个存储桶。

我必须每隔30分钟将它们装箱。