How to group dataframe by hour using timestamp with Pandas(如何使用带有 Pandas 的时间戳按小时对数据帧进行分组)
问题描述
我有以下使用时间戳索引的数据帧结构:
I have the following dataframe structure that is indexed with a timestamp:
neg neu norm pol pos date
time
1520353341 0.000 1.000 0.0000 0.000000 0.000
1520353342 0.121 0.879 -0.2960 0.347851 0.000
1520353342 0.217 0.783 -0.6124 0.465833 0.000
我根据时间戳创建一个日期:
I create a date from the timestamp:
data_frame['date'] = [datetime.datetime.fromtimestamp(d) for d in data_frame.time]
结果:
neg neu norm pol pos date
time
1520353341 0.000 1.000 0.0000 0.000000 0.000 2018-03-06 10:22:21
1520353342 0.121 0.879 -0.2960 0.347851 0.000 2018-03-06 10:22:22
1520353342 0.217 0.783 -0.6124 0.465833 0.000 2018-03-06 10:22:22
我想按小时分组,同时获得除时间戳以外的所有值的平均值,应该是小时小组开始的地方.所以这是我要归档的结果:
I want to group by hour, while getting the mean for all the values, except the timestamp, that should be the hour from where the group started. So this is the result I want to archive:
neg neu norm pol pos
time
1520352000 0.027989 0.893233 0.122535 0.221079 0.078779
1520355600 0.028861 0.899321 0.103698 0.209353 0.071811
到目前为止,我得到的最接近的是这个 回答:
The closest I have gotten so far has been with this answer:
data = data.groupby(data.date.dt.hour).mean()
结果:
neg neu norm pol pos
date
0 0.027989 0.893233 0.122535 0.221079 0.078779
1 0.028861 0.899321 0.103698 0.209353 0.071811
但我不知道如何保留考虑到 grouby 开始时间的时间戳.
But I cant figure out how to keep the timestamp that takes in account he hour where the grouby started.
推荐答案
我遇到了这个 gem,pd.DataFrame.resample,在我发布了按小时计算的解决方案之后.
I came across this gem, pd.DataFrame.resample, after I posted my round-to-hour solution.
# Construct example dataframe
times = pd.date_range('1/1/2018', periods=5, freq='25min')
values = [4,8,3,4,1]
df = pd.DataFrame({'val':values}, index=times)
# Resample by hour and calculate medians
df.resample('H').median()
或者你可以使用 groupby 与 Grouper 如果您不想将时间作为索引:
Or you can use groupby with Grouper if you don't want times as index:
df = pd.DataFrame({'val':values, 'times':times})
df.groupby(pd.Grouper(level='times', freq='H')).median()
这篇关于如何使用带有 Pandas 的时间戳按小时对数据帧进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用带有 Pandas 的时间戳按小时对数据帧进行分组
基础教程推荐
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
