繁体   English   中英

使用时间增量比较 Pandas Dataframe 中的行值

[英]Compare row values in Pandas Dataframe over using time delta

我正在尝试比较时间间隔内的行数据,而不是行迭代。 即我想将一个值与它在 'X' 分钟前的可比值进行比较。

我的数据框中的每一行都不是标准化的时间增量。

具体来说,我想将此子集数据帧中的heading值与 2 分钟前的heading值进行比较。

我尝试了一些方法,例如 timedeltas 和 .shift() 方法,但到目前为止没有任何乐趣,我自己也很困惑。 任何想法或帮助将不胜感激。

index                heading               times
2015-12-09 03:00:01      NaN 2015-12-09 03:00:01
2015-12-09 03:01:07   231.12 2015-12-09 03:01:07
2015-12-09 03:01:08     0.00 2015-12-09 03:01:08
2015-12-09 03:01:10    90.00 2015-12-09 03:01:10
2015-12-09 03:01:15    90.00 2015-12-09 03:01:15
2015-12-09 03:02:22   149.23 2015-12-09 03:02:22
2015-12-09 03:02:25     0.00 2015-12-09 03:02:25
2015-12-09 03:02:32   270.00 2015-12-09 03:02:32
2015-12-09 03:02:40      NaN 2015-12-09 03:02:40
2015-12-09 03:02:42    90.00 2015-12-09 03:02:42
2015-12-09 03:02:48   270.00 2015-12-09 03:02:48
2015-12-09 03:03:15     9.39 2015-12-09 03:03:15
2015-12-09 03:03:17   210.77 2015-12-09 03:03:17
2015-12-09 03:03:35   153.61 2015-12-09 03:03:35
2015-12-09 03:03:39    90.00 2015-12-09 03:03:39
2015-12-09 03:03:40   263.84 2015-12-09 03:03:40
2015-12-09 03:03:46   351.30 2015-12-09 03:03:46
2015-12-09 03:03:48   270.00 2015-12-09 03:03:48
2015-12-09 03:03:50   267.69 2015-12-09 03:03:50
2015-12-09 03:03:51   270.00 2015-12-09 03:03:51
2015-12-09 03:04:10   205.03 2015-12-09 03:04:10
2015-12-09 03:04:11    90.00 2015-12-09 03:04:11
2015-12-09 03:04:12   270.00 2015-12-09 03:04:12
2015-12-09 03:04:18      NaN 2015-12-09 03:04:18
2015-12-09 03:04:24     0.00 2015-12-09 03:04:24

我一直在使用以下内容(有 2 分钟的差异):

def diff(ref):
    refvalue = ref['heading']
    selection = data.index - ref.name == np.timedelta64(2, 'm')
    values = data['heading'].loc[selection] - refvalue
    return values.mean()

data['diff'] = data.apply(diff, axis=1)

但这不是很有效,因为它进行了 O(n^2) 比较。 例如,可以跳过一半的条目,因为它们是将来的。

我希望有人可以提出更好的建议。

这可以通过reindex函数实现,请参见

在 Pandas 中按增量时间移动行

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reindex.html

也许你想要 method='nearest'。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM