Python Pandas:将日期时间列分组为小时和分钟聚合 [英] Python Pandas: Group datetime column into hour and minute aggregations

查看:113
本文介绍了Python Pandas:将日期时间列分组为小时和分钟聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎相当简单,但在将近一整天之后我还没有找到解决方案.我已经用 read_csv 加载了我的数据框,并且很容易地将日期和时间列解析、组合和索引到一列中,但现在我希望能够根据小时和分钟分组来重塑和执行计算,类似于你可以做的excel 支点.

This seems like it would be fairly straight forward but after nearly an entire day I have not found the solution. I've loaded my dataframe with read_csv and easily parsed, combined and indexed a date and a time column into one column but now I want to be able to just reshape and perform calculations based on hour and minute groupings similar to what you can do in excel pivot.

我知道如何重新采样到小时或分钟,但它保留了与每个小时/分钟相关的日期部分,而我只想将数据集聚合到小时和分钟,类似于在 excel 数据透视表中分组并选择小时"和分钟"但不选择其他任何内容.

I know how to resample to hour or minute but it maintains the date portion associated with each hour/minute whereas I want to aggregate the data set ONLY to hour and minute similar to grouping in excel pivots and selecting "hour" and "minute" but not selecting anything else.

任何帮助将不胜感激.

推荐答案

你不能,df 是你的 DataFrame:

Can't you do, where df is your DataFrame:

times = pd.to_datetime(df.timestamp_col)
df.groupby([times.hour, times.minute]).value_col.sum()

这篇关于Python Pandas:将日期时间列分组为小时和分钟聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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