如何从Pandas数据框中找到最近24小时的数据

2024-04-26 13:49:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据,其中有两列,一列是description,另一列是publishedAt。我对publishedAt列应用了sort函数,得到了日期降序的输出。以下是我的数据帧示例:

        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小时。 我怎么才能找到它?在

谢谢


Tags: theto数据函数timeisdescriptionsort
1条回答
网友
1楼 · 发布于 2024-04-26 13:49:01

假设数据帧名为df

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

如果您需要间隔互斥的,因此在过去6小时内的描述,而不是在3小时内的描述,那么您将需要在第一个breaket中使用类似于numpy的numpy.logicaland(arr1, arr2)的类似数组的逻辑运算符。在

相关问题 更多 >