3 回答
TA贡献1847条经验 获得超11个赞
感谢您添加数据和示例输出。首先,我很确定您不能对它进行矢量化处理,因为每个计算都取决于上一个的输出。所以这是我所能做到的最好的。
您的方法大约0.116999在我的机器上几秒钟
这个大约在0.0039999几秒钟之内
没有向量化,但是速度得到了很好的提高,因为为此使用列表并将其添加回末尾的数据帧更快。
def myfunc(pos_pre, signal):
if signal == 0: # if the signal is negative
# if the new col value above -0.2 is > -1 then subtract 0.2 from that value
pos = pos_pre - 0.02
if pos < -1: # if the row above - 0.2 < -1 set the value of current row to -1
pos = -1
elif signal == 1:
# if the row above + 0.2 < 1 then add 0.2 to the value of the current row
pos = pos_pre + 0.02
if pos > 1: # if the value above + 0.1 > 1 set the current row to 1
pos = 1
return pos
''' set first position value because you aren't technically calculating it correctly in your method since there is no
position minus 1... IE: it will always be 0.02'''
new_pos = [0.02]
# skip index zero since there is no position 0 minus 1
for i in range(1, len(df)):
new_pos.append(myfunc(pos_pre=new_pos[i-1], signal=df['signal'].iloc[i]))
df['position'] = new_pos
输出:
df.position
0 0.02
1 0.04
2 0.06
3 0.08
4 0.10
5 0.12
6 0.14
7 0.16
8 0.18
TA贡献1806条经验 获得超8个赞
不要使用循环。熊猫专门从事矢量化运算,例如signal == 0:
pos_shift = df['position'].shift() - 0.02
m1 = df['signal'] == 0
m2 = pos_shift < -1
df.loc[m1 & m2, 'position'] = -1
df['position'] = np.where(m1 & ~m2, pos_shift, df['position'])
您可以为编写类似的内容signal == 1。
TA贡献1898条经验 获得超8个赞
有很多更好的方法,但是这种方法也应该起作用:
df['previous'] = df.signal.shift()
def get_signal_value(row):
if row.signal == 0:
compare = row.previous - 0.02
if compare < -1:
return -1
else:
return compare
elif row.signal == 1:
compare = row.previous + 0.01
if compare > 1:
return 1
else:
return compare
df['new_signal'] = df.apply(lambda row: get_signal_value(row), axis=1)
添加回答
举报