Search code examples
pythonpandasdataframefor-loopcounter

Counter for consecutive negative values in a Data-frame


I need to implement a counter, which does the counting as shown in the below OUTPUT. It checks the past values of "data" column for negative values.

    data    output
0   -1      Nan        //  since there are no past values for data: count=NaN 
1   -2       1         //-1, so count= 1
2    4       2         //-2,-1   count=2
3    12      0         //         count=0
4   -22      0         //         count=0    
5   -12      1         //-22      count=1          
6   -7       2         // -22,-12   count=2     
7   -5       3         // -7,-22,-12    count=3
8   -33      4         // -5,7,-22,-12    count=4
9    2       5         // -33,-5,7,-22,-12    count=5
10   2       1         //        count=0

MY CODE

import pandas as pd
import talib
import numpy as np     

df=pd.DataFrame()
df["data"]=[-1,-2,4,12,-22,-12,-7,-5,-33,2,2]
print(df)


c=0
for y in [0,len(ff)-1] : 
    for z in [1,10]:
        if (ff["data"].shift(-z)).any()<=0:c=c+1
        else:c
        if (ff["data"].shift(-z)).any()>0:break
    count["dd"]=c

OUTPUT needed:

enter image description here


Solution

  • I am pretty unsure how to write the "Nan" (not very great myself), but here is a code that seems to do what you asked for:

    df = pd.DataFrame()
    df["data"] = [-1, -2, 4, 12, -22, -12, -7, -5, -22, 2, 2]
    def generateOutput(df):
        a = [0]
        for i in range(len(df) - 1):
            if df["data"][i] < 0:
                a.append(a[-1] + 1)
            else:
                a.append(0)
        df["output"] = a
        return df
    
    print(df)
    df = generateOutput(df)
    print(df)
    

    And here is my output when launched the program

        data
    0     -1
    1     -2
    2      4
    3     12
    4    -22
    5    -12
    6     -7
    7     -5
    8    -22
    9      2
    10     2
    
        data  output
    0     -1       0
    1     -2       1
    2      4       2
    3     12       0
    4    -22       0
    5    -12       1
    6     -7       2
    7     -5       3
    8    -22       4
    9      2       5
    10     2       0