Python 从数据集中筛选出连续行
我有一个具有索引值和一个datetime变量的数据集,如下所示:Python 从数据集中筛选出连续行,python,pandas,Python,Pandas,我有一个具有索引值和一个datetime变量的数据集,如下所示: 1 2017-01-03 09:30:01.958 46 2017-01-03 09:30:47.879 99 2017-01-03 09:33:48.121 117 2017-01-03 09:47:06.215 139 2017-01-03 09:51:06.054 1567 2017-01-03 14:17:18.949 2480 2017-01-03 15:57:13.442
1 2017-01-03 09:30:01.958
46 2017-01-03 09:30:47.879
99 2017-01-03 09:33:48.121
117 2017-01-03 09:47:06.215
139 2017-01-03 09:51:06.054
1567 2017-01-03 14:17:18.949
2480 2017-01-03 15:57:13.442
2481 2017-01-03 15:57:14.333
2486 2017-01-03 15:57:37.500
2487 2017-01-03 15:57:38.677
2489 2017-01-03 15:57:41.053
2491 2017-01-03 15:57:54.870
2498 2017-01-03 15:59:24.210
我试图做的是从数据中删除连续的行(只保留段中的第一个观察值),在这种情况下,代码应该删除索引为2481和2487的行。我试着用
df[df.index.diff() == 0].drop()
但它只是回来了
AttributeError: 'Int64Index' object has no attribute 'diff'
对于使用未实现的方法使用索引
,您可以使用:
感谢您提供numpy备选方案:
df[np.append(0, np.diff(df.index.values)) != 1]
计时:
#[11000 rows x 1 columns]
df = pd.concat([df]*1000)
In [60]: %timeit [True] + [(i[0]+1) != i[1] for i in zip(df.index.tolist(), df.index.tolist()[1:])]
100 loops, best of 3: 4.19 ms per loop
In [61]: %timeit np.append(0, np.diff(df.index.values)) != 1
The slowest run took 4.72 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 33.1 µs per loop
In [62]: %timeit df.index.to_series().diff() != 1
1000 loops, best of 3: 260 µs per loop
对于使用未实现的方法使用索引
,您可以使用:
感谢您提供numpy备选方案:
df[np.append(0, np.diff(df.index.values)) != 1]
计时:
#[11000 rows x 1 columns]
df = pd.concat([df]*1000)
In [60]: %timeit [True] + [(i[0]+1) != i[1] for i in zip(df.index.tolist(), df.index.tolist()[1:])]
100 loops, best of 3: 4.19 ms per loop
In [61]: %timeit np.append(0, np.diff(df.index.values)) != 1
The slowest run took 4.72 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 33.1 µs per loop
In [62]: %timeit df.index.to_series().diff() != 1
1000 loops, best of 3: 260 µs per loop
更多
numpy
df[np.append(True, df.index.values[1:] != df.index.values[:-1] + 1)]
date
1 2017-01-03 09:30:01.958
46 2017-01-03 09:30:47.879
99 2017-01-03 09:33:48.121
117 2017-01-03 09:47:06.215
139 2017-01-03 09:51:06.054
1567 2017-01-03 14:17:18.949
2480 2017-01-03 15:57:13.442
2486 2017-01-03 15:57:37.500
2489 2017-01-03 15:57:41.053
2491 2017-01-03 15:57:54.870
2498 2017-01-03 15:59:24.210
更多
numpy
df[np.append(True, df.index.values[1:] != df.index.values[:-1] + 1)]
date
1 2017-01-03 09:30:01.958
46 2017-01-03 09:30:47.879
99 2017-01-03 09:33:48.121
117 2017-01-03 09:47:06.215
139 2017-01-03 09:51:06.054
1567 2017-01-03 14:17:18.949
2480 2017-01-03 15:57:13.442
2486 2017-01-03 15:57:37.500
2489 2017-01-03 15:57:41.053
2491 2017-01-03 15:57:54.870
2498 2017-01-03 15:59:24.210
我知道你们袖子里有东西。有趣的是,[df.index.to_series().diff()!=1]将返回相同的内容。我非常喜欢可读性,更喜欢您的版本,但是我做了一个时间比较,为什么速度会慢得多?是to_series()转换吗?答案相同,但数值化了。
df[np.append(0,np.diff(df.index.values))!=1]
应该加快转换速度。@AntonvBR-测试samm数据帧时出现了问题,越大,速度越快。您能将我的答案添加到计时中吗?谢谢,我知道你们袖子里有东西。有趣的是,[df.index.to_series().diff()!=1]将返回相同的内容。我非常喜欢可读性,更喜欢您的版本,但是我做了一个时间比较,为什么速度会慢得多?是to_series()转换吗?答案相同,但数值化了。df[np.append(0,np.diff(df.index.values))!=1]
应该加快转换速度。@AntonvBR-测试samm数据帧时出现了问题,越大,速度越快。您能将我的答案添加到计时中吗?谢谢