python中的Dicing

2024-05-16 15:49:06 发布

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

在代码:-在

df = pd.DataFrame({'col1':t, 'col2':wordList})
df.columns=['DNT','tweets']
df.DNT = pd.to_datetime(df.DNT, errors='coerce')
check=df[ (df.DNT < '09:20:00') & (df.DNT > '09:00:00') ]

不知道为什么这个代码不起作用。 有人知道上面的代码有什么问题吗?在


Tags: columnsto代码dataframedfdatetimechecktweets
1条回答
网友
1楼 · 发布于 2024-05-16 15:49:06

您可以这样与datetime格式进行比较:

假设:

import pandas as pd
import datetime
df = pd.DataFrame({'col1':['2017-05-24 09:06:11','2017-05-24 09:06:12','2017-05-24 09:00:00'], 'col2':['hello','hi','bonjour']})
df.columns=['DNT','tweets']
df.DNT = pd.to_datetime(df.DNT, errors='coerce')
df

df将是:

^{pr2}$

然后可以与startend进行比较:

end = datetime.datetime(2017, 5, 24, 9, 20) #2017-05-24 09:20:00
start = datetime.datetime(2017, 5, 24, 9) #2017-05-24 09:00:00
df[ (df.DNT < end) & (df.DNT > start) ]

则过滤结果为:

    DNT                  tweets
0   2017-05-24 09:06:11  hello
1   2017-05-24 09:06:12  hi

相关问题 更多 >