我有一个指示方向的数据框:
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
我要基于以下条件(从1/25/19开始)创建“动量”列:
1.如果相应日期的“方向”为“向上”,则将值设置为“向上”
2.如果动量下方的第一行是“向上”,则将其设置为“向上”
3.如果相应日期的方向为“向下”,则将其设置为“无”
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
是否有一种无需使用循环即可完成此操作的方法?
答案 0 :(得分:2)
这是一种方法。喝咖啡后,我会尝试改善它的。
df['Momentum:'] = None # Base case.
df.loc[df['Direction:'].eq('Upward'), 'Momentum:'] = 'Upward'
df.loc[df['Direction:'].eq('Downward'), 'Momentum:'] = 1 # Temporary value.
df.loc[:, 'Momentum:'] = df['Momentum:'].bfill()
df.loc[df['Momentum:'].eq(1), 'Momentum:'] = None # Set temporary value back to None.
>>> df
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
答案 1 :(得分:2)
通过新数据编辑答案,首先回填None
值,然后将Downward
替换为None
s:
#first replace strings Nones to None type
df['Direction:'] = df['Direction:'].mask(df['Direction:'] == 'None', None)
df['Momentum:'] = df['Direction:'].bfill().mask(lambda x: x == 'Downward', None)
或者:
s = df['Direction:'].bfill()
df['Momentum:'] = s.mask(s == 'Downward', None)
print (df)
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
旧答案:
将numpy.where
与链接的布尔掩码比较移位后的值一起使用,并且还按|
进行按位OR的原始操作:
mask = df['Direction:'].eq('Upward') | df['Direction:'].shift(-1).eq('Upward')
df['Momentum:'] = np.where(mask, 'Upward', None)
print (df)
Direction: Momentum:
1/31/19 None Upward
1/30/19 Upward Upward
1/29/19 None None
1/28/19 None None
1/27/19 Downward None
1/26/19 None Upward
1/25/19 Upward Upward