Pandas conditional cumulative sum
<p>I'm working with data that has null values in between. I intend to construct a time-series plot out of cumulative sum of a particular column <code>sales</code>. Conditions for cumulative sum on <code>sales</code>: (1.) if first row is null, <code>fillna(0)</code>, then <code>cumsum()</code> so plot can always start from origin. (2.) if null rows follow each other to the end, leave as null else <code>fillna(0)</code>:</p>

<pre><code>data = {'year': [2010, 2011, 2012, 2013, 2014, 2015, 2016,2017, 2018, 2019],
        'quantity': [10, 21, 20, 10, 39, 30, 31,45, 23, 56],
        'sales': [None, 41, None, None, 32, 0, 31,None, None, None]}

df = pd.DataFrame(data)
df = df.set_index('year')

df['cum_sales'] = df[['sales']].cumsum()

print df
df.plot()
</code></pre>

<p><a href="https://i.stack.imgur.com/4qVaC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4qVaC.png" alt="enter image description here"></a></p>

<p>How to apply conditions such that result becomes:</p>

<p><a href="https://i.stack.imgur.com/qutl2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qutl2.png" alt="enter image description here"></a></p>