pandas: Keep only every row that has cumulated change by a threshold?
<p>I'm interested to extract the rows where a column's value has either gone up cumulatively by at least 5 or gone down cumulatively by at least 5, then get the signs of these cumulative changes, <code>up_or_down</code>.</p>

<p>For example, let's say I want to apply this to column <code>y</code> in the following:</p>

<pre><code>df = pd.DataFrame({'x': range(16), 'y': [1,10,14,12,13,9,4,2,6,7,10,11,16,17,14,11]})
</code></pre>

<p>It should yield:</p>

<pre><code>x   y        # up_or_down
1   10       # +1
6   4        # -1
10  10       # +1
12  16       # +1
15  11       # -1
</code></pre>

<p>My dataframe is quite large so I was hoping that there is a nice vectorized way to do this natively with pandas's API rather than looping through it with <code>iterrows()</code>.</p>

<p>You can't get it vectorized through the standard functions pandas exposed: the nth point to find moving by +/-5 from n-1th is dynamically found and will depend on the position of n-1th, which itself depends on the n-2 first dynamically determined points. Thus there is no math associated to rolling or expanding set of functions that can project you in a vector space requiring this dynamism. So you have to write you iteration ad-hoc. </p>

python pandas numpy