根据其他数据框的条件和过去的(t-1)值创建数据框



我想根据来自其他两个数据框(相同形状)的值创建一个新的数据框,并且,比较其中一个数据框的t和t-1(之前)的值。

我有两个数据帧:p和PR。第三个数据帧的逻辑是:如果(P_t>= 30.6 &PR_t

P = pd.DataFrame({'col1': [26.7,24.7,26.1,26.4,24.5], 'col2': [30.8,30.8,30.7,30.8,29.8], 'col3': [30.8,30.7,30.5,30.6,30.0]})
PR = pd.DataFrame({'col1': [79.8,73.6,81.1,79.4,75.7], 'col2': [74.1,74.1,77.0,74.7,74.1], 'col3': [74.0,74.0,76.4,74.3,74.8]})

将给我一个结果数据帧,如:

pd.DataFrame({'col1': [0,0,0,0,0], 'col2': [0,1,0,1,0], 'col3': [0,1,0,0,0]})

任何帮助都非常感谢!

您可以使用:

(P.ge(30.6) & PR.diff().lt(0)).astype(int)

输出:

col1  col2  col3
0     0     0     0
1     0     0     0
2     0     0     0
3     0     1     1
4     0     0     0

最新更新