我有一个带方向的数据框:
Direction:
2/01/19 None
1/31/19 Upward
1/30/19 None
1/29/19 None
1/28/19 Downward
1/27/19 None
1/26/19 None
1/25/19 Upward
我想根据以下条件创建一个“动量”列(从19年1月25日开始):
1。如果相应日期的方向为“向上”,则将值设置为“向上”
2如果动量中下面的第一行是“向上”,则将其设置为“向上”
三。如果相应日期的方向为“向下”,则将其设置为“无”
4否则,将其设置为“无”
换言之,一旦你达到“向上”的状态,它应该一直保持这种状态,直到你达到“向下”的状态
结果应该如下所示:
Direction: Momentum:
2/01/19 None Upward
1/31/19 Upward Upward
1/30/19 None None
1/29/19 None None
1/28/19 Downward None
1/27/19 None Upward
1/26/19 None Upward
1/25/19 Upward Upward
有没有不使用循环的方法来完成这个任务?你知道吗
由新数据编辑的答案首先填充
None
值,然后将Downward
替换为None
:或:
旧答案:
将^{} 与链式布尔掩码一起使用,比较移位值,并将原始值按
|
用于按位或:这里有一个方法。喝点咖啡后我会努力改进的。。。你知道吗
相关问题 更多 >
编程相关推荐