如何有条件地选择要显示的行

时间:2019-08-12 04:53:21

标签: python pandas dataframe

我有一个数据框:

Detection Location  Detection Date & Time   
CV234   22/09/2018 5:16:07 PM
CV23    23/09/2018 3:01:12 AM
CV23    23/09/2018 7:06:44 PM   
CV23    30/09/2018 9:52:35 PM   
CV23    23/09/2018 8:46:25 AM   
CV23    23/09/2018 10:52:54 AM  
CV23    23/09/2018 5:47:44 AM   
CV234   13/09/2018 6:16:58 PM   
CV23    8/09/2018 2:24:19 PM
CV23    9/09/2018 1:45:46 PM
CV234   7/09/2018 10:26:59 PM
CV23    8/09/2018 1:12:42 AM
CV23    8/09/2018 4:18:17 PM
CV23    7/09/2018 9:32:54 AM

我只想有条件地拾取那些在1小时内检测到3个值的行。就像该小时内,“检测位置”列中应该有3个值。

1 个答案:

答案 0 :(得分:0)

您需要将列转换为日期时间,然后使用DataFrame.resampleResampler.transformResampler.size来计算与原始DataFrame相同大小的Series每小时的值数量用boolean indexing过滤:

df['Detection Date & Time'] = pd.to_datetime(df['Detection Date & Time'])

s = df.resample('H',on='Detection Date & Time')['Detection Location'].transform('size')

df1 = df[s.sort_index() == 3]
print (df1)