如何根据 pandas 中的时间值过滤数据?

问题描述:

我是Python的新手.我正在研究pandas软件包,以分析工作负荷模式.我的数据框包含两列,分别是工作的开始时间和结束时间.我想在特定的开始时间和结束时间之间过滤数据帧的行.例如,上午7:00:00之后和下午18:00:00前.我尝试使用以下内容:

I am new to Python. I am working on pandas package to analyse the work load pattern. My dataframe contains two columns with start and end time of work. I would like to filter rows of dataframe between particular start time and end time. For e.g after 7:00:00 am and before 18:00:00 pm. I tried using the following :

(df['STime'] > '7:00:00') & (df['ETime'] < '18:00:00') 

但是它为所有行返回False.

But it returns False for all rows.

您正在比较值,这就是为什么您只获得False值的原因 将此表达式与数据框一起使用.

You are just comparing the values that's why you are getting only False values Use this expression with dataframe.

 df=df[(df['STime'] > '7:00:00') and (df['ETime'] < '18:00:00')]