【发布时间】:2022-07-31 09:06:58
【问题描述】:
我想用pandasrolling函数比较第一个元素是否小于第二个。我认为以下代码应该可以工作:
import numpy as np
import pandas as pd
df = pd.DataFrame(data=np.random.randint(0,10,10), columns=['temperature'])
df.rolling(window=2).apply(lambda x: x[0] < x[1])
但它不起作用。相反,我收到一条错误消息:
ValueError: 0 is not in range
有人知道是什么原因造成的吗?
更新:
我知道我可以使用diff 函数,但我真正想做的是这样的
df.rolling(window=3).apply(lambda x: x[0] < x[1] < x[2])
【问题讨论】: