简体   繁体   English

选择当前行中列值为 1 但上一行中值为 0 的行

[英]Selecting rows where column value is 1 in the current row, but 0 in the previous row

I am working with a DataFrame on Python 3.8 where I try to replicate Excel calculations - a basic if with two criteria, one of which is referencing itself a row before.我正在使用 Python 3.8 上的 DataFrame 来尝试复制 Excel 计算 - 一个基本的if之前有两个标准,其中一个是引用自身。

Backtest['trade_price']=0
Backtest.loc[(Backtest['z_en_crit']==1) & 
             (Backtest['trade_price'].shift(-1)==0), "trade_price"] = 1

The second criterion seems to be completely ignored... as trade_price should have only one 1第二个标准似乎完全被忽略了......因为trade_price应该只有一个1

                           z_en_crit  trade_price
Datetime                                         
2020-10-21 11:00:00+01:00        0.0            0
2020-10-21 12:00:00+01:00        0.0            0
2020-10-21 13:00:00+01:00        1.0            1
2020-10-21 14:00:00+01:00        1.0            1
2020-10-21 15:00:00+01:00        1.0            1
2020-10-21 16:00:00+01:00        0.0            0

If you have any idea how to make this work and also make it faster I'd really appreciate it!如果您知道如何使这项工作变得更快,我将不胜感激! Thanks!谢谢!

EDIT:编辑:

Required output:所需 output:

                           z_en_crit  trade_price
Datetime                                         
2020-10-21 11:00:00+01:00        0.0            0
2020-10-21 12:00:00+01:00        0.0            0
2020-10-21 13:00:00+01:00        1.0            1
2020-10-21 14:00:00+01:00        1.0            0
2020-10-21 15:00:00+01:00        1.0            0
2020-10-21 16:00:00+01:00        0.0            0

Are you looking to shift on "z_en_crit" instead?您是否希望转而使用“z_en_crit”? Also, you should reverse the direction of the shift if you want to match on the first of the group, not the last.此外,如果要匹配组的第一个而不是最后一个,则应反转移位的方向。

df['trade_price'] = np.where(
    df['z_en_crit'].eq(1) & df['z_en_crit'].shift(1).eq(0), 1, 0)

df

                           z_en_crit  trade_price
Datetime                                         
2020-10-21 11:00:00+01:00        0.0            0
2020-10-21 12:00:00+01:00        0.0            0
2020-10-21 13:00:00+01:00        1.0            1
2020-10-21 14:00:00+01:00        1.0            0
2020-10-21 15:00:00+01:00        1.0            0
2020-10-21 16:00:00+01:00        0.0            0

暂无
暂无

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

相关问题 用 Pandas 中的前一行列填充当前行和列值 - Fill current row and column value with previous row column in Pandas 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row 获取列值从上一行发生变化的行的索引 - Get index of row where column value changes from previous row 有没有办法根据现有列的当前和前一行值在每行的新列中分配增量值的数量? - Is there a way to assign number of incremental value in a new column of each rows based current and previous row values of existing columns? 添加到列表中,当前行某列的值a DataFrame 仅当前面的行通过测试 - Add to the list, a value of a column of the current row of a DataFrame only if the previous rows pass the test 用前几行的值减去当前行的值并返回较大的值 - Subtract value in current row with previous rows values and return the greater value 根据“ID”列返回上一行值以查找当前行与上一行之间的差异 - Return previous row value based on "ID" column to find differences between current row and previous row 将前面的N行与pandas列中的当前行进行比较 - Compare the previous N rows to the current row in a pandas column Select 行按列值并按另一个列值包含前一行 - Select rows by column value and include previous row by another column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM