w3resource

Pandas Series: sum() function

Sum of the values for the requested axis in Pandas

The sum() function is used to getg the sum of the values for the requested axis.

This is equivalent to the method numpy.sum.

Syntax:

Series.sum(self, axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)
Pandas Series sum image

Parameters:

Name Description Type/Default Value Required / Optional
axis Axis for the function to be applied on. {index (0)} Required
skipna Exclude NA/null values when computing the result. bool
Default Value: True
Required
level If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar. int or level name
Default Value: None
Required
numeric_only Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series. bool
Default Value: None
Required
min_count The required number of valid values to perform the operation. If fewer than min_count non-NA values are present the result will be NA.
New in version 0.22.0: Added with the default being 0. This means the sum of an all-NA or empty Series is 0, and the product of an all-NA or empty Series is 1.
int
Default Value: 0
Required
**kwargs Additional keyword arguments to be passed to the function. Required

Returns: scalar or Series (if level specified)

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['fox', 'cat', 'snake', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 4, 0, 8], name='legs', index=idx)
s

Output:

  blooded  animal
warm     fox       4
         cat       4
cold     snake     0
         spider    8
Name: legs, dtype: int64
Pandas Series sum image

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['fox', 'cat', 'snake', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 4, 0, 8], name='legs', index=idx)
s.sum()

Output:

16

Example - Sum using level names, as well as indices:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['fox', 'cat', 'snake', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 4, 0, 8], name='legs', index=idx)
s.sum(level='blooded')

Output:

  blooded
warm    8
cold    8
Name: legs, dtype: int64

Example - By default, the sum of an empty or all-NA Series is 0:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['fox', 'cat', 'snake', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 4, 0, 8], name='legs', index=idx)
pd.Series([]).sum()  # min_count=0 is the default

Output:

0.0

Example - This can be controlled with the min_count parameter. For example, if you’d like the sum of an empty series to be NaN, pass min_count=1:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['fox', 'cat', 'snake', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 4, 0, 8], name='legs', index=idx)
pd.Series([]).sum(min_count=1)

Output:

nan

Example - Thanks to the skipna parameter, min_count handles all-NA and empty series identically:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['fox', 'cat', 'snake', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 4, 0, 8], name='legs', index=idx)
pd.Series([np.nan]).sum()

Output:

0.0

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['fox', 'cat', 'snake', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 4, 0, 8], name='legs', index=idx)
pd.Series([np.nan]).sum(min_count=1)

Output:

nan

Previous: Compute numerical data ranks along axis
Next: Unique values of Series object in Pandas



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-sum.php