这是清理我的数据的问题。数据框如下所示:
timestamp
0 10
1 12
2 23
3 25
4 27
5 34
6 45
我打算做的是从上到下遍历时间戳,如果没有使用以前的时间戳,则抓取一个(对于初始化,它将采用'10'),然后省略时间戳在 [10, 10 之间的每一行+10],包括“12”。同样,我们应该取 '23' 并省略 '25'、'27',因为它们在 [23、23+10] 之间。最后,'34' 和 '45' 也应该被取走。
最终的结果是
timestamp
0 10
2 23
5 34
6 45
谁能给我一些在 Pandas 中实现这一点的想法?非常感谢!
请您参考如下方法:
我认为没有办法使用类似 groupby 的构造来解决这个自定义问题,但这里有一个编码解决方案,可以为您提供索引位置和时间戳值。
stamps = [df.timestamp.iat[0]]
index = [df.index[0]]
for idx, ts in df.timestamp.iteritems():
if ts >= stamps[-1] + 10:
index.append(idx)
stamps.append(ts)
>>> index
[0, 2, 5, 6]
>>> stamps
[10, 23, 34, 45]
>>> df.iloc[index]
timestamp
0 10
2 23
5 34
6 45
