Part II: Counting how many times in a row the result of a sum is positive (or negative)
<p><strong>Second part</strong>  First part can be found here: <a href="http://stackoverflow.com/questions/39514202/counting-how-many-times-in-a-row-the-result-of-a-sum-is-positive-or-negative">Click me</a></p>

<p>Hi all, I have been practising with the gg function that you guys help me create -- see part one. Now, I realized that the output of the function are not unique series, yet a sum: for instance, a series of 3 positives in a row is also shown as 2 series of two positives in a row and as 3 single positives.</p>

<p>Let's say I got this:</p>

<pre><code>df = pd.DataFrame(np.random.rand(15, 2), columns=["open", "close"])
df['test'] = df.close-df.open &gt; 0

        open     close   test
0   0.769829  0.261478  False
1   0.770246  0.128516  False
2   0.266448  0.346099   True
3   0.302941  0.065790  False
4   0.747712  0.730082  False
5   0.382923  0.751792   True
6   0.028505  0.083543   True
7   0.137558  0.243148   True
8   0.456349  0.649780   True
9   0.041046  0.163488   True
10  0.291495  0.617486   True
11  0.046561  0.038747  False
12  0.782994  0.150166  False
13  0.435168  0.080925  False
14  0.679253  0.478050  False

df.test
Out[113]: 
0     False
1     False
2      True
3     False
4     False
5      True
6      True
7      True
8      True
9      True
10     True
11    False
12    False
13    False
14    False
</code></pre>

<p>As output, I would like the unique number of series of True in a row; something like:</p>

<pre><code>1: 1
2: 0
3: 0
4: 0
5: 0
6: 1
7: 0
8: 0
</code></pre>

<p>What I've tried so far:</p>

<pre><code>(green.rolling(x).sum()&gt;x-1).sum() #gives me how many times there is a series of x True in a row; yet, this is not unique as explained beforehand
</code></pre>

<p>However, I do not feel the rolling is the solution over here...</p>

<p>Thank you again for your help,
CronosVirus00</p>

<p>What you are looking for are the <code>groupby</code> function <strong>from <code>itertools</code></strong> and <code>Counter</code> from <code>collections</code>. Here is how to achieve what you want :</p>

<pre><code>import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(15, 2), columns=["open", "close"])
df['test'] = df.close-df.open &gt; 0 

from itertools import groupby
from collections import Counter
#we group each sequence of True and False
seq_len=[(k,len(list(g))) for k, g in groupby(list(df['test']))]
#we filter to keep only True sequence lenght
true_seq_len= [n for k,n in seq if k == True]
#we count each length
true_seq_count = Counter(true_seq_len)
</code></pre>

<p>Output :</p>

<pre><code>&gt;&gt;&gt; print(df['test'])
0      True
1      True
2     False
3      True
4      True
5     False
6      True
7     False
8      True
9      True
10     True
11     True
12    False
13    False
14     True
&gt;&gt;&gt;print(seq_len)
[(True, 2), (False, 1), (True, 2), (False, 1), (True, 1), (False, 1), (True, 4), (False, 2), (True, 1)]
&gt;&gt;&gt;print(true_seq_count)
Counter({1: 2, 2: 2, 4: 1})
</code></pre>

python pandas numpy