Python numpy nonzero cumsum
<p>I want to do nonzero <code>cumsum</code> with <code>numpy</code> array. Simply skip zeros in array and apply <code>cumsum</code>. Suppose I have a np. array </p>

<pre><code>a = np.array([1,2,1,2,5,0,9,6,0,2,3,0])
</code></pre>

<p>my result should be</p>

<pre><code>[1,3,4,6,11,0,20,26,0,28,31,0]
</code></pre>

<p>I have tried this</p>

<pre><code>a = np.cumsum(a[a!=0])
</code></pre>

<p>but result is</p>

<pre><code>[1,3,4,6,11,20,26,28,31]
</code></pre>

<p>Any ideas? </p>

<p>Just trying to simplify it:)</p>

<pre><code>b=np.cumsum(a)
[b[i]  if ((i &gt; 0 and b[i] != b[i-1]) or i==0) else 0 for i in range(len(b))]
</code></pre>

python numpy