根据结果减去两个datetime64列并删除行



我有一个数据帧,看起来像这个

trip_id     start_date  start_station_id    end_date    end_station_id  subscription_type   journey_duration    weekday
0   913460  2019-08-31 23:26:00     50  2019-08-31 23:39:00     70  Subscriber  0 days 00:13:00     Sat
1   913459  2019-08-31 23:11:00     31  2019-08-31 23:28:00     27  Subscriber  0 days 00:17:00     Sat
2   913455  2019-08-31 23:13:00     47  2019-08-31 23:18:00     64  Subscriber  0 days 00:05:00     Sat
3   913454  2019-08-31 23:10:00     10  2019-08-31 23:17:00     8   Subscriber  0 days 00:07:00     Sat
4   913453  2019-08-31 23:09:00     51  2019-08-31 23:22:00     60  Customer    0 days 00:13:00     Sat

本质上我使用

trip_data['journey_duration'] = trip_data['end_date'] - trip_data['start_date']

为了获得行程持续时间,现在我想删除行程持续时间超过36小时的行

我尝试过,但没有成功

trip_data2 = trip_data[(trip_data['journey_duration'] < 1days 12:00:00) ].copy()

如有任何建议,将不胜感激

感谢

尝试:

# convert to datetime:
df["start_date"] = pd.to_datetime(df["start_date"])
df["end_date"] = pd.to_datetime(df["end_date"])
# get only rows where the time difference is less than 36*60*60 seconds (36 hours): 
df_out = df[
(df["end_date"] - df["start_date"]).dt.total_seconds() < 36 * 60 * 60
]
print(df_out)

最新更新