Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据帧中两个时间值之间的行_Python_Pandas_Numpy - Fatal编程技术网

Python 数据帧中两个时间值之间的行

Python 数据帧中两个时间值之间的行,python,pandas,numpy,Python,Pandas,Numpy,我有一个像这样的数据帧,时间戳转换为datetime对象: id timestamp 0 221 2020-11-07 12:02:00 1 223 2020-11-08 13:21:00 2 224 2020-11-09 12:50:00 3 225 2020-11-10 14:23:00 4 226 2020-11-11 12:25:00 5 227 2020-11-14 14:26:00 我想找出一个时间间隔之间的行数。e、 g 12:00-13:00,

我有一个像这样的数据帧,时间戳转换为datetime对象:

    id  timestamp
0   221 2020-11-07 12:02:00
1   223 2020-11-08 13:21:00
2   224 2020-11-09 12:50:00
3   225 2020-11-10 14:23:00
4   226 2020-11-11 12:25:00
5   227 2020-11-14 14:26:00

我想找出一个时间间隔之间的行数。e、 g 12:00-13:00,这里是3个条目0、2和4,因为我认为建议的只适用于DatetimeIndex,您可以将数据帧索引设置为“timestamp”,然后在时间之间使用,或者,首先使用

然后使用

然后找到行数

count = start - end

因为我相信建议的只适用于DatetimeIndex,所以您可以将数据帧索引设置为“timestamp”,然后使用between_time,或者,首先使用

然后使用

然后找到行数

count = start - end
请试一试

强制时间戳为日期时间并按升序排序

  df['timestamp']=pd.to_datetime(df['timestamp']).sort_values(ascending=True)
重置索引但不删除以保留id

df.reset_index(drop=False, inplace=True)
将时间戳设置为新索引,以允许使用df.between时间

df.set_index(df['timestamp'], inplace=True)
df.between_time('12:00', '13:00')
请试一试

强制时间戳为日期时间并按升序排序

  df['timestamp']=pd.to_datetime(df['timestamp']).sort_values(ascending=True)
重置索引但不删除以保留id

df.reset_index(drop=False, inplace=True)
将时间戳设置为新索引,以允许使用df.between时间

df.set_index(df['timestamp'], inplace=True)
df.between_time('12:00', '13:00')

正如评论部分的帖子所建议的那样,这种方法很有效。首先需要将timestamp(此处为datetime64对象)作为索引,然后使用Interween_time函数。

正如评论部分的帖子所建议的那样,效果很好。首先需要将timestamp(此处为datetime64对象)作为索引,然后使用between_time函数。

正如我所评论的,我认为您可以使用函数:

CountRows = df.set_index('timestamp').between_time('12:00','13:00').shape[0]
在您的例子中,它将列timestamp设置为索引,然后返回两个timevalue之间的行数。其中:

Dataframe.shape返回的元组的第一个元素包含 数据帧中索引中的项数,即基本上是 数据帧中的行数


因此,正如我所评论的,我认为您可以只使用函数:

CountRows = df.set_index('timestamp').between_time('12:00','13:00').shape[0]
在您的例子中,它将列timestamp设置为索引,然后返回两个timevalue之间的行数。其中:

Dataframe.shape返回的元组的第一个元素包含 数据帧中索引中的项数,即基本上是 数据帧中的行数


看起来会有帮助这是一个关于两人之间时间的例子,你有没有尝试过什么,做过什么研究?这回答了你的问题吗?在两人之间,时间会起作用的。只需将时间戳设置为索引。看起来会很有帮助。这是一个关于时间间隔的示例,您尝试过什么,做过什么研究吗?这回答了您的问题吗?在两人之间,时间会起作用的。只需将时间戳设置为索引。