Python pandas nonzero cumsum
<p>I want to apply <code>cumsum</code> on dataframe in <code>pandas</code> in <code>python</code>, but withouth zeros. Simply I want to leave zeros and do <code>cumsum</code> on dataframe. Suppose I have dataframe like this:</p>

<pre><code>import pandas as pd

df =  pd.DataFrame({'a' : [1,2,0,1], 
                    'b' : [2,5,0,0], 
                    'c' : [0,1,2,5]})

   a  b  c
0  1  2  0
1  2  5  1
2  0  0  2
3  1  0  5
</code></pre>

<p>and result sould be</p>

<pre><code>   a  b  c
0  1  2  0
1  3  7  1
2  0  0  3
3  4  0  8
</code></pre>

<p>Any ideas how to do that avoiding loops? In <code>R</code> there is <code>ave</code> function, but Im very new to <code>python</code> and I dont know.</p>

<p>You can mask the df so that you only overwrite the non-zero cells:</p>

<pre><code>In [173]:
df[df!=0] = df.cumsum()
df

Out[173]:
   a  b  c
0  1  2  0
1  3  7  1
2  0  0  3
3  4  0  8
</code></pre>

python pandas