一列Pandas数据框的时差(以小时为单位) [英] Time difference in hours of a column of pandas dataframe

查看:56
本文介绍了一列Pandas数据框的时差(以小时为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

id  time_taken
1   2017-06-21 07:36:53
2   2017-06-21 07:32:28
3   2017-06-22 08:55:09
4   2017-06-22 08:04:31
5   2017-06-21 03:38:46

current_time = 2017-06-22 10:08:16

我想创建df2,其中time_taken列的时差与current_time大于24小时

i want to create df2 where time difference of time_taken columns is greater than 24 hours with current_time

1   2017-06-21 07:36:53
2   2017-06-21 07:32:28
5   2017-06-21 03:38:46

推荐答案

您可以将Timedelta转换为

You can convert Timedelta to total_seconds and compare or compare with Timedelta, filter by boolean indexing:

current_time = '2017-06-22 10:08:16'

df['time_taken'] = pd.to_datetime(df['time_taken'])


df = df[(pd.to_datetime(current_time) - df['time_taken']).dt.total_seconds() > 60 * 60 * 24]
print (df)
   id          time_taken
0   1 2017-06-21 07:36:53
1   2 2017-06-21 07:32:28
4   5 2017-06-21 03:38:46

或者:

df = df[(pd.to_datetime(current_time) - df['time_taken']) > pd.Timedelta(24, unit='h')]
print (df)
   id          time_taken
0   1 2017-06-21 07:36:53
1   2 2017-06-21 07:32:28
4   5 2017-06-21 03:38:46

这篇关于一列Pandas数据框的时差(以小时为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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