如何从pandas数据框中查找最近24小时的数据

时间:2018-05-07 07:28:50

标签: python datetime

我有一个数据,其中我们有两列,一个是描述,另一个是已发布。我在publishedAt列上应用了sort函数并获得了date的降序输出。以下是我数据框的示例:

        description publishedAt
13  Bitcoin price has failed to secure momentum in...   2018-05-06T15:22:22Z
16  Brian Kelly, a long-time contributor to CNBC’s...   2018-05-05T15:56:48Z
2   The bitcoin price is less than $100 away from ...   2018-05-05T13:14:45Z
12  Mati Greenspan, a senior analyst at eToro and ...   2018-05-04T16:05:37Z
52  A Singaporean startup developing ‘smart bankno...   2018-05-04T14:02:30Z
75  Cryptocurrencies are set to make a comeback on...   2018-05-03T08:10:19Z
76  The bitcoin price is hovering near its best le...   2018-04-30T16:26:57Z
74  In today’s climate of ICOs with 100 billion to...   2018-04-30T12:03:31Z
27  Investment guru Warren Buffet remains unsold o...   2018-04-29T17:22:19Z
22  The bitcoin price has increased by around $400...   2018-04-28T12:28:35Z
68  Bitcoin futures volume reached an all-time hig...   2018-04-27T16:32:44Z
14  Biotech-company-turned-cryptocurrency-investme...   2018-04-27T14:25:15Z
67  The bitcoin price has rebounded to $9,200 afte...   2018-04-27T06:24:42Z

现在我要描述最近3小时,6小时,12小时和24小时。 我怎么能找到它?

由于

1 个答案:

答案 0 :(得分:2)

假设数据框名为df

import datetime as dt
df[df['publishedAt']>=(dt.datetime.now()-dt.timedelta(hours=3))]['description'] #hours = 6,12, 24

如果您需要间隔独占,因此描述过去6小时而不是3小时内的那些,您需要在第一个分区中使用类似numpy.logicaland(arr1, arr2)的numpy类似逻辑运算符。