Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/pandas/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/google-chrome-extension/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何创建使用前几行(如触发器事件)的数据帧?_Python_Pandas - Fatal编程技术网

Python 如何创建使用前几行(如触发器事件)的数据帧?

Python 如何创建使用前几行(如触发器事件)的数据帧?,python,pandas,Python,Pandas,我尝试了这段代码,但没有达到我想要的效果。我正在尝试让事件在熊猫数据帧上工作。如果过期日期等于索引日期,则我希望值为0。如果预测列显示为1,那么我希望得到1值。我希望它检查前一行是否已经触发了1,直到过期日期触发了0为止,它将一直保持为1 df['Forecast'] = np.select([df.ExpirationDate == df.index, df.Predictions == 1,

我尝试了这段代码,但没有达到我想要的效果。我正在尝试让事件在熊猫数据帧上工作。如果过期日期等于索引日期,则我希望值为0。如果预测列显示为1,那么我希望得到1值。我希望它检查前一行是否已经触发了1,直到过期日期触发了0为止,它将一直保持为1

df['Forecast'] = np.select([df.ExpirationDate == df.index,
                         df.Predictions == 1,
                         df.Forecast.shift(1).eq(1)], [0, 1, 1])
我试过换档,但它似乎不能正常工作,因为它只能换档一次

                 Open        High  ...  ExpirationDate  Predictions
Date                                ...                             
2017-09-18  249.610001  250.119995  ...      2017-10-20            0
2017-09-19  250.000000  250.070007  ...      2017-10-20            0
2017-09-20  250.070007  250.190002  ...      2017-10-20            0
2017-09-21  249.880005  249.979996  ...      2017-10-20            0
2017-09-22  249.050003  249.630005  ...      2017-10-20            0
预期产量

            Predictions  Forecast  
Date                               
2017-09-18            0         0  
2017-09-19            0         0  
2017-09-20            0         0  
2017-09-21            0         0  
2017-09-22            0         0  
2017-09-25            0         0  
2017-09-26            0         0  
2017-09-27            0         0  
2017-09-28            0         0  
2017-09-29            0         0  
2017-10-02            0         0  
2017-10-03            0         0  
2017-10-04            0         0  
2017-10-05            1         1  
2017-10-06            0         1  
2017-10-09            0         1  
2017-10-10            0         1  
2017-10-11            0         1  
2017-10-12            0         1  
2017-10-13            0         1  
2017-10-16            0         1  
2017-10-17            0         1  
2017-10-18            0         1  
2017-10-19            0         1  
2017-10-20            0         0

因此,预测中出现1后,预测将保持1。当到期时,Forecast将返回到0。

我们可以使用
系列。其中
用于屏蔽,允许使用
fillna向前填充。另一个
,其中
仅在每行的过期日期之前设置值,我们将剩余值设置回0

df['Forecast'] = (df['Predictions'].where(df.Predictions.eq(1))
                       .ffill()
                       .where(df.index < df.ExpirationDate)
                       .fillna(0, downcast='infer'))
我们也可以使用+:


你能发布你的预期输出吗?我希望预期输出能帮助解释我想要得到的东西。这真的很接近。唯一的问题是,当日期与截止日期相同时,预测仍然得到1而不是0。这个答案也非常接近,并且有相同的问题,因为当截止日期与索引日期相同时,它没有给出0。@frostd检查是NaN
'd。你确定你没有使用是的<和否
            Predictions ExpirationDate  Forecast
Date                                            
2017-09-18            0     2017-10-20         0
2017-09-19            0     2017-10-20         0
2017-09-20            0     2017-10-20         0
2017-09-21            0     2017-10-20         0
2017-09-22            0     2017-10-20         0
2017-09-25            0     2017-10-20         0
2017-09-26            0     2017-10-20         0
2017-09-27            0     2017-10-20         0
2017-09-28            0     2017-10-20         0
2017-09-29            0     2017-10-20         0
2017-10-02            0     2017-10-20         0
2017-10-03            0     2017-10-20         0
2017-10-04            0     2017-10-20         0
2017-10-05            1     2017-10-20         1
2017-10-06            0     2017-10-20         1
2017-10-09            0     2017-10-20         1
2017-10-10            0     2017-10-20         1
2017-10-11            0     2017-10-20         1
2017-10-12            0     2017-10-20         1
2017-10-13            0     2017-10-20         1
2017-10-16            0     2017-10-20         1
2017-10-17            0     2017-10-20         1
2017-10-18            0     2017-10-20         1
2017-10-19            0     2017-10-20         1
2017-10-20            0     2017-10-20         0
df['Forecast']=(

df['Predictions'].cumsum()
                 .clip(0,1)
                 .where(df.index < df.ExpirationDate)
                 .fillna(0, downcast='infer')
)
print(df)
            Predictions ExpirationDate  Forecast
Date                                            
2017-09-18            0     2017-10-20         0
2017-09-19            0     2017-10-20         0
2017-09-20            0     2017-10-20         0
2017-09-21            0     2017-10-20         0
2017-09-22            0     2017-10-20         0
2017-09-25            0     2017-10-20         0
2017-09-26            0     2017-10-20         0
2017-09-27            0     2017-10-20         0
2017-09-28            0     2017-10-20         0
2017-09-29            0     2017-10-20         0
2017-10-02            0     2017-10-20         0
2017-10-03            0     2017-10-20         0
2017-10-04            0     2017-10-20         0
2017-10-05            1     2017-10-20         1
2017-10-06            0     2017-10-20         1
2017-10-09            0     2017-10-20         1
2017-10-10            0     2017-10-20         1
2017-10-11            0     2017-10-20         1
2017-10-12            0     2017-10-20         1
2017-10-13            0     2017-10-20         1
2017-10-16            0     2017-10-20         1
2017-10-17            0     2017-10-20         1
2017-10-18            0     2017-10-20         1
2017-10-19            0     2017-10-20         1
2017-10-20            0     2017-10-20         0