比较不同数据帧之间的日期时间
我有两个不同的数据框。
df1=
index Time
0 2009-09-13 01:17:00
1 2009-09-13 02:17:00
2 2009-09-13 03:17:00
3 2009-09-13 04:17:00
4 2009-09-13 05:17:00
............
这是一个时间序列数据,每小时间隔总共 10 天。 我
df2=
Report Time x
0 2009-09-13 01:17:00 1
1 2009-09-13 02:20:00 27
2 2009-09-13 02:25:00 1
3 2009-09-13 05:33:00 100
..............
想通过“df2”迭代“df1”每小时时间数据,并将“x”求和为其每小时值。然后每小时的数据将存储x值。
输出应如下所示:
index Time x
0 2009-09-13 01:17:00 1
1 2009-09-13 02:17:00 28
2 2009-09-13 03:17:00 0
3 2009-09-13 04:17:00 0
4 2009-09-13 05:17:00 100
请注意,数据集“df1”的所有 x 值应在同一小时内添加在一起。
我尝试实现 for 循环但无法完成。
I have two different data frames.
df1=
index Time
0 2009-09-13 01:17:00
1 2009-09-13 02:17:00
2 2009-09-13 03:17:00
3 2009-09-13 04:17:00
4 2009-09-13 05:17:00
............
This is a time-series data with hourly interval spanning in total 10 days.
and
df2=
Report Time x
0 2009-09-13 01:17:00 1
1 2009-09-13 02:20:00 27
2 2009-09-13 02:25:00 1
3 2009-09-13 05:33:00 100
..............
I want to iterate the "df1" hourly time data through "df2" and do the summation of "x" to its hourly value. Then The hourly data will store the x value.
The output should look like this:
index Time x
0 2009-09-13 01:17:00 1
1 2009-09-13 02:17:00 28
2 2009-09-13 03:17:00 0
3 2009-09-13 04:17:00 0
4 2009-09-13 05:17:00 100
Note that all of the x value should be added together within the same hour for the dataset "df1".
I have tried to implement for loop but couldn't complete it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
DatetimeIndex.floor
获取两个数据帧之间的相同时间:输出:
详细信息:
Use
DatetimeIndex.floor
to get same time between the 2 dataframes:Output:
Details:
您需要执行
merge_asof
:输出:
You need to perform a
merge_asof
:Output: