How does the series.cumsum() method work in Pandas?

PandasServer Side ProgrammingProgramming

<p>The pandas Series.cumsum() method is used to find the cumulative sum of the elements in a series object.</p><p>The Series.cumsum() method returns a cumulative sum with the same length as the original series object. The first element of the cumulative sum is the same as the input object.</p><p>This method has three parameters which are &ldquo;axis&rdquo;, &ldquo;skipna&rdquo; and &ldquo;args&rdquo; keywords. The important parameter is &ldquo;skipna&rdquo; which is used to exclude Nan/null values by default, if we include the missing values then we need to set it to &ldquo;False&rdquo;.</p><h2>Example 1</h2><pre class="demo-code notranslate language-python3" data-lang="python3"># importing required packages import pandas as pd import numpy as np # create a pandas Series object series = pd.Series([9,3,8,np.nan,4]) print(series) print(&quot;Cumulative sum: &quot;,series.cumsum())</pre><h2>Explanation</h2><p>In this example, we are finding the cumulative sum of the series object &ldquo;series&rdquo;, which is having some integer values and Nan. Here, we have applied the cumsum() method without changing the default parameter values.</p><h2>Output</h2><pre class="result notranslate">0 9.0 1 3.0 2 8.0 3 NaN 4 4.0 dtype: float64 Cumulative sum: 0 &nbsp;9.0 1 12.0 2 20.0 3 &nbsp;NaN 4 24.0 dtype: float64</pre><p>The first element of the cumulative sum has the same element as the original series object. The cumsam() method skips the Nan values by default so that the Nan value at index position 3 is ignored.</p><h2>Example 2</h2><pre class="demo-code notranslate language-python3" data-lang="python3"># importing required packages import pandas as pd import numpy as np # create a pandas Series object series = pd.Series([7,-3,18,np.nan,4,1]) print(series) print(&quot;Cumulative sum including NA: &quot;,series.cumsum(skipna=False))</pre><h2>Explanation</h2><p>Same as the previous example, here also we calculated the cumulative sum, But the skipna parameter is changed to False from default True. Hence NULL values won&rsquo;t be ignored.</p><h2>Output</h2><pre class="result notranslate">0 &nbsp;7.0 1 -3.0 2 18.0 3 &nbsp;NaN 4 &nbsp;4.0 5 &nbsp;1.0 dtype: float64 Cumulative sum including NA: 0 &nbsp;7.0 1 &nbsp;4.0 2 22.0 3 NaN 4 NaN 5 NaN dtype: float64</pre><p>Up to the Nan value, we got the cumulative sum elements. After that we got only Nan value, this is because the cumulative sum of NaN with anything that will be NaN only.</p>
raja
Updated on 09-Mar-2022 09:15:48

Advertisements