计算数字从正数变为负数的日子

Counting Days where numbers turn from positive to negative

我有以下数据框:

macd_hist
Out[10]: 
            ADANIPORTS.NS  ASIANPAINT.NS  ...  WIPRO.NS   ZEEL.NS
Date                                      ...                    
2015-06-22            NaN            NaN  ...       NaN       NaN
2015-06-23            NaN            NaN  ...       NaN       NaN
2015-06-24            NaN            NaN  ...       NaN       NaN
2015-06-25            NaN            NaN  ...       NaN       NaN
2015-06-26            NaN            NaN  ...       NaN       NaN
                  ...            ...  ...       ...       ...
2020-06-12      -0.064481       1.635353  ...  0.213215 -1.800832
2020-06-15      -0.702969       0.135702  ... -0.096160 -3.020285
2020-06-16      -1.125824      -0.567845  ... -0.438076 -3.804984
2020-06-17      -1.423891      -2.635996  ... -0.347506 -4.095071
2020-06-18      -1.497237      -3.613468  ... -0.312098 -3.520918

[1227 rows x 50 columns]

我如何计算每个代码列的数字从正数变为负数的天数。因此,如果昨天的数字是正数,今天变成负数,那就是 1,但它不应该计数,直到它再次变为负数,然后变成正数,然后再次变成负数,这将是另一个计数。

我要计算的是:

如果我做对了,你可以试试下面的方法:

((macd_hist > 0).astype(int).diff() > 0).sum()

让我们分解一下。它将执行以下操作:

  • (macd_hist >= 0) : 检查你的号码是否为正数
  • .astype(int) : 转换成整数
  • .diff():检测变化(-1 表示 pos 到 neg,否则 1)
  • < 0 : 只保留从 pos 到 neg
  • 的变化
  • .sum() : 统计这种变化的次数

您可以使用 enumerate():

d = [-1,-2,-1,1,2,4,1,-1,-2,-4,-1,3,4,5,2,-2,-3,-1,3,4,3,1,-1,-3,-2,-1,2,3,4]

count = 0
for i,n in enumerate(d):
    if i < len(d)-1 and d[i] > 0 and d[i+1] < 0:
        count += 1
print(count)

输出:

3

我会计算过零。在下面的解决方案中进行了尝试,但我无法统计。每个代码中只有一个零交叉。我的逻辑是,从每个自动收报机中获取零交叉并将其分配为 1,否则为零。 cumsumcumcount

第 1 部分

 #Zerocrossing

a=df.ZEEL.lt(0)
c1=a.ne(a.shift(1))

b=df.WIPRO.lt(0)
c2=b.ne(b.shift(1))

c=df.ASIANPAINT.lt(0)
c3=c.ne(c.shift(1))

d=df.ADANIPORTS.lt(0)
c4=d.ne(d.shift(1))



 Attribute in columns

df['ADANIPORTSZC']=np.where(c4,1,0)
df['ASIANPAINTZC']=np.where(c3,1,0)
df['WIPROZC']=np.where(c2,1,0)
df['ZEELZC']=np.where(c1,1,0)




 Date  ADANIPORTS  ASIANPAINT     WIPRO      ZEEL  ADANIPORTSZC  \
0  2015-06-22    0.000000    0.000000  0.000000  0.000000             1   
1  2015-06-23    0.000000    0.000000  0.000000  0.000000             0   
2  2015-06-24    0.000000    0.000000  0.000000  0.000000             0   
3  2015-06-25    0.000000    0.000000  0.000000  0.000000             0   
4  2015-06-26    0.000000    0.000000  0.000000  0.000000             0   
5  2020-06-12   -0.064481    1.635353  0.213215 -1.800832             1   
6  2020-06-15   -0.702969    0.135702 -0.096160 -3.020285             0   
7  2020-06-16   -1.125824   -0.567845 -0.438076 -3.804984             0   
8  2020-06-17   -1.423891   -2.635996 -0.347506 -4.095071             0   
9  2020-06-18   -1.497237   -3.613468 -0.312098 -3.520918             0   

   ASIANPAINTZC  WIPROZC  ZEELZC  
0             1        1       1  
1             0        0       0  
2             0        0       0  
3             0        0       0  
4             0        0       0  
5             0        0       1  
6             0        1       0  
7             1        0       0  
8             0        0       0  
9             0        0       0 

如果只需要代码的零交叉。一行代码就可以做到。基本上用代码、布尔值 select 对行进行切片并将布尔值转换为 integreg

df.iloc[:,1::].apply(lambda x:x.le(0).ne(x.le(0).shift(1))).astype(int)



     ADANIPORTS  ASIANPAINT  WIPRO  ZEEL
0           1           1      1     1
1           0           0      0     0
2           0           0      0     0
3           0           0      0     0
4           0           0      0     0
5           0           1      1     0
6           0           0      1     0
7           0           1      0     0
8           0           0      0     0
9           0           0      0     0