假设我在 Pandas 数据框中有两列时间序列数据,分别为“a”和“b”。我想创建第三列,指示当前时间段的“a”列与接下来 5 个时间段中的任何一个的“b”列之间的差异是否增加了 8 或更多,然后减少了 2 或更多。理想情况下,我会使用某种形式的 df.rolling(5).apply() 并且没有任何循环,但我一直遇到挑战。
为了演示起见,我用循环写出了逻辑,但如果有人能给我一些指导,告诉我如何更有效或更优雅地做到这一点,我将不胜感激。实际上,数据框和窗口会大得多。
df = pd.DataFrame({'a':[1,2,3,4,5,6,7,8,9,10], 'b':[1,0,9,0,15,0,20,15,23,6]})
df['c'] = 0
window = 5
positive_thresh = 8
negative_thresh = -2
num_rows = df.shape[0]
for a_idx in range(num_rows):
a_start = df.iloc[a_idx,0]
b_roll = df.iloc[(a_idx + 1):max(a_idx + 1 + window,num_rows), 1]
deltas = b_roll - a_start
positives = deltas[deltas>=positive_thresh]
negatives = deltas[deltas<=negative_thresh]
first_pos_idx = positives.index[0] if len(positives) > 0 else num_rows
first_neg_idx = negatives.index[0] if len(negatives) > 0 else num_rows
if first_pos_idx < first_neg_idx:
df.iloc[a_idx,2] = 1
print(df)
a b c
0 1 1 1
1 2 0 0
2 3 9 0
3 4 0 1
4 5 15 0
5 6 0 1
6 7 20 1
7 8 15 1
8 9 23 0
9 10 6 0