我想使此循环明显更快。它正在为每个壮举者连续计算移动。此处的功能仅适用于一列。稍后,我遍历每个功能(df.columns)并应用此功能。
def move_iar(df, feature):
lst=[]
prev_move_iar = 0
for move in df[feature]:
if np.isnan(move):
move_iar = 0
lst.append(move_iar)
prev_move_iar = move_iar
else:
if move == 0:
move_iar = prev_move_iar
lst.append(move_iar)
prev_move_iar = move_iar
elif (move >= 0 and prev_move_iar >= 0) or (move <= 0 and prev_move_iar <= 0):
move_iar = move + prev_move_iar
lst.append(move_iar)
prev_move_iar = move_iar
elif (move < 0 and prev_move_iar >= 0) or (move > 0 and prev_move_iar <= 0):
move_iar = move
lst.append(move_iar)
prev_move_iar = move_iar
return pd.DataFrame(lst, index=df.index, columns=[feature]).rename(columns={feature : feature + 'IAR'})