我有一个包含此数据的pandas数据框:
Id Converteddate Createddate
0015000000toohpAAA 2015-07-24 00:00:00 2014-07-08 19:36:13
0015000000tqEpKAAU 2015-03-17 00:00:00 2014-07-16 00:28:06
00138000015me01AAA 2015-10-22 00:00:00 2015-10-22 22:04:55
00138000015me56AAA 2015-10-22 00:00:00 2015-10-22 22:17:52
我试图仅保留ConvertedDate< = CreatedDate + 3天的行,因此我将两个字符串转换为datetime,然后使用timedelta计算3天。我没有收到任何错误代码,但我的输出数据框保留的记录不符合我的ConvertedDate< = CreatedDate + 3天的条件。
netnewframe['CreatedDate'] = netnewframe.apply(lambda row: ToDateTimeObj(row['CreatedDate']), axis=1)
netnewframe['ConvertedDate'] = netnewframe.apply(lambda row: ToDateTimeObj(row['ConvertedDate']), axis=1)
netnewframe = netnewframe[(netnewframe.ConvertedDate <= (netnewframe.CreatedDate + timedelta(days=3)))]
答案 0 :(得分:1)
您可以用于转换为datetime
函数to_datetime
,然后使用to_timedelta
:
netnewframe['CreatedDate'] = pd.to_datetime(netnewframe['CreatedDate'])
netnewframe['ConvertedDate'] = pd.to_datetime(netnewframe['ConvertedDate'])
netnewframe = netnewframe[netnewframe.ConvertedDate <= (netnewframe.CreatedDate +
pd.to_timedelta(3, unit='d'))]
print (netnewframe)
Id ConvertedDate CreatedDate
2 00138000015me01AAA 2015-10-22 2015-10-22 22:04:55
3 00138000015me56AAA 2015-10-22 2015-10-22 22:17:52