user633549
user633549

Reputation: 21

pandas how to get current Number of consecutive positive number?

pandas how to get current Number of consecutive positive number?

In Python Pandas, I have a data frame with columns and records in the following format:

In [7]: d = {'x' : [1,-1,1,1,-1,1,1,1,-1,1,1,1,1,-1,1,1,1,1,1]}

In [8]: df = pd.DataFrame(d)

In [9]: df
Out[9]:
    x
0   1
1  -1
2   1
3   1
4  -1
5   1
6   1
7   1
8  -1
9   1
10  1
11  1
12  1
13 -1
14  1
15  1
16  1
17  1
18  1

how can I get get current Number of consecutive positive number?

for example, I want result Like this(add a column y to represent consecutive positive number)

    x  y
0   1  1
1  -1  0
2   1  1
3   1  2
4  -1  0
5   1  1
6   1  2
7   1  3
8  -1  0
9   1  1
10  1  2
11  1  3
12  1  4
13 -1  0
14  1  1
15  1  2
16  1  3
17  1  4
18  1  5

Upvotes: 2

Views: 529

Answers (2)

piRSquared
piRSquared

Reputation: 294258

pandas
Clumsy, but should work

p = df.y > 0
c = p.cumsum()
c - c.mask(p).ffill().fillna(0).astype(int)

Upvotes: 2

snapcrack
snapcrack

Reputation: 1811

Try this. Also I used a random jumble of 1's and not 1's, it's different than your data:

          x
    0     1
    1    -1
    2     1
    3     1
    4     1
    5    -1
    6     1
    7     1
    8     1
    9     1
    10   -1


y = [] #Create a list outside a counter function

    def count(df):
        counter = 0
        for item in df:
            if item > 0:
                counter += 1
                y.append(counter)
            else:
                counter = 0
                y.append(counter)
        return y

count(df['x']) #run function

df['y'] = y #add column based on list


      y
0     1
1     0
2     1
3     2
4     3
5     0
6     1
7     2
8     3
9     4
10    0

Upvotes: 0

Related Questions