本文介绍了在循环中删除 pandas 数据帧之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想递归删除位于定义的时间增量之间的数据帧行-如图所示.

I would like to recursively drop dataframe rows that are located between defined time delta - as shown in Figure.

我准备了以下代码段:

import pandas as pd

time_series = pd.date_range('2018-01-01', periods=100, freq='ms')
df = pd.Series(range(len(time_series)), index=time_series)
print(df)
df = df.drop(df.between_time("00:00:00.003", "00:00:00.098").index)

过滤的时间范围应该与日期无关,并且仅考虑小时差异.我应该如何删除图中显示的放置"部分中的不必要数据?循环应该一直持续到包含大约1亿行的数据框的结尾.

The range of time filtered should be date independent and take into account only difference in hour. How should I drop non necessary data located in "drop" sections presented in Figure? The loop should be until the end of dataframe that contains about 100 millions of rows.

推荐答案

您可以尝试:

i = pd.date_range('2018-01-01', periods=100, freq='ms')
df = pd.DataFrame({'A': range(100)}, index=i)
df.drop(df.between_time(*pd.to_datetime(['00:00:00.003', '00:00:00.098']).time).index, inplace=True)

结果:

                          A
2018-01-01 00:00:00.000   0
2018-01-01 00:00:00.001   1
2018-01-01 00:00:00.002   2
2018-01-01 00:00:00.099  99

这篇关于在循环中删除 pandas 数据帧之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:13