数据帧中时间戳之间的时间差作为直方图

2024-06-28 19:36:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,其中一列都是时间戳,如下所示。我现在需要做的是计算每个时间戳之间的差异,然后使用这些差异绘制直方图。 我无法解释如何计算差异。任何帮助都将不胜感激

0       2020-09-16 00:00:02.713264
1       2020-09-16 00:00:02.827854
2       2020-09-16 00:00:05.919288
3       2020-09-16 00:00:05.940775
4       2020-09-16 00:00:06.682184

enter image description here


Tags: 数据时间绘制差异直方图无法解释
1条回答
网友
1楼 · 发布于 2024-06-28 19:36:51

给定一个虚拟df

# df
#                    timestamp
# 0 2020-09-16 00:00:02.713264
# 1 2020-09-16 00:00:02.827854
# 2 2020-09-16 00:00:05.919288
# 3 2020-09-16 00:00:05.940775
# 4 2020-09-16 00:00:06.682184

你应该能够使用

ax = df['timestamp'].diff().dropna().dt.total_seconds().plot.hist()
ax.set_xlabel('timedelta[s]')

…这应该会产生一个像 enter image description here

相关问题 更多 >