我有两列要比较每第n行。如果遇到第n行,它将对其进行比较,并将if语句的结果放在新列中。

当我尝试枚举函数时,它总是以if语句的真实部分结尾。不知何故,这段代码总是存在:

如果(count%3)== 0:

for count, factors in enumerate(df.index):
    if (count % 3)== 0: #every 3th row
        df['Signal']=np.where(df['Wind Ch']>=df['Rain Ch'],'1', '-1')
    else:
        df['Signal']=0


在“信号”列中,我希望每第3行为“ 1”或“ -1”,而在所有其他行上为“ 0”。但是我在每一行上得到'1'或'-1'

现在我得到:

            Date  Wind CH Rain CH  Signal
    0   5/10/2005  -1.85%  -3.79%       1
    1   5/11/2005   1.51%  -1.66%       1
    2   5/12/2005   0.37%   0.88%      -1
    3   5/13/2005  -0.81%   3.83%      -1
    4   5/14/2005  -0.28%   4.05%      -1
    5   5/15/2005   3.93%   1.79%       1
    6   5/16/2005   6.23%   0.94%       1
    7   5/17/2005  -0.08%   4.43%      -1
    8   5/18/2005  -2.69%   4.02%      -1
    9   5/19/2005   6.40%   1.33%       1
    10  5/20/2005  -3.41%   2.38%      -1
    11  5/21/2005   3.27%   5.46%      -1
    12  5/22/2005  -4.40%  -4.15%      -1
    13  5/23/2005   3.27%   4.48%      -1


但我想得到:

              Date  Wind CH Rain CH  Signal
     0   5/10/2005  -1.85%  -3.79%     0.0
     1   5/11/2005   1.51%  -1.66%     0.0
     2   5/12/2005   0.37%   0.88%    -1.0
     3   5/13/2005  -0.81%   3.83%     0.0
     4   5/14/2005  -0.28%   4.05%     0.0
     5   5/15/2005   3.93%   1.79%     1.0
     6   5/16/2005   6.23%   0.94%     0.0
     7   5/17/2005  -0.08%   4.43%     0.0
     8   5/18/2005  -2.69%   4.02%    -1.0
     9   5/19/2005   6.40%   1.33%     0.0
     10  5/20/2005  -3.41%   2.38%     0.0
     11  5/21/2005   3.27%   5.46%    -1.0
     12  5/22/2005  -4.40%  -4.15%     0.0
     13  5/23/2005   3.27%   4.48%     0.0


我在这里想念什么?

最佳答案

您可以这样处理,使用np.vectorize避免循环:

import numpy as np

def calcSignal(x, y, i):
    return 0 if (i + 1) % 3 != 0 else 1 if x >= y else -1

func = np.vectorize(calcSignal)

df['Signal'] = func(df['Wind CH'], df['Rain CH'], df.index)

df

         Date  Wind CH Rain CH  Signal
0   5/10/2005  -1.85%  -3.79%       0
1   5/11/2005   1.51%  -1.66%       0
2   5/12/2005   0.37%   0.88%      -1
3   5/13/2005  -0.81%   3.83%       0
4   5/14/2005  -0.28%   4.05%       0
5   5/15/2005   3.93%   1.79%       1
6   5/16/2005   6.23%   0.94%       0
7   5/17/2005  -0.08%   4.43%       0
8   5/18/2005  -2.69%   4.02%      -1
9   5/19/2005   6.40%   1.33%       0
10  5/20/2005  -3.41%   2.38%       0
11  5/21/2005   3.27%   5.46%      -1
12  5/22/2005  -4.40%  -4.15%       0
13  5/23/2005   3.27%   4.48%       0

关于python - 用枚举在第n行中做某事,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58197650/

10-11 21:18