time difference between timestamps in a pandas dataframe as histogram

别等时光非礼了梦想. 提交于 2021-01-29 09:46:31

问题


I have a data frame with one column that is all timestamps like below. What I need to do is now calculate the difference between each of the timestamps and then use those differences to plot as a histogram. I am unable to decipher how to do the calculation on the differences. Any help would be appreciated.

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


回答1:


given a dummy 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

you should be able to use

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

...which should spawn a plot like



来源:https://stackoverflow.com/questions/64063517/time-difference-between-timestamps-in-a-pandas-dataframe-as-histogram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!