Conditional length of a binary data series in Pandas
<p>Having a DataFrame with the following column:</p>

<pre><code>df['A'] = [1,1,1,0,1,1,1,1,0,1]
</code></pre>

<p>What would be the best vectorized way to control the length of "1"-series by some limiting value? Let's say the limit is 2, then the resulting column 'B' must look like:</p>

<pre><code>   A  B
0  1  1
1  1  1
2  1  0
3  0  0
4  1  1
5  1  1
6  1  0
7  1  0
8  0  0
9  1  1
</code></pre>

<p>Another way (checking if previous two are 1):</p>

<pre><code>In [443]: df = pd.DataFrame({'A': [1,1,1,0,1,1,1,1,0,1]})

In [444]: limit = 2

In [445]: df['B'] = map(lambda x: df['A'][x] if x &lt; limit else int(not all(y == 1 for y in df['A'][x - limit:x])), range(len(df)))

In [446]: df
Out[446]: 
   A  B
0  1  1
1  1  1
2  1  0
3  0  0
4  1  1
5  1  1
6  1  0
7  1  0
8  0  0
9  1  1
</code></pre>

python pandas vectorization