Pandas: Series.sum() method – Tutorial & Examples

In this article we will discuss the sum() function of Series class in Pandas in detail.

Series.sum() Syntax:

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

It gives the sum of values in the Series object. But based on parameters we can control its behavior.

Parameters:

  • axis: Default value 0 (Index axis).
    • It represents the axis along which sum function will be applied
  • skipna: bool, Default value is True.
    • If True then skip NaNs while calculating the sum.
  • level: int or level name. The default value is None
    • If the axis is Multi-Index, then add items in the given level only
  • numeric_only: bool. The default value is None
    • If True then include only int, float or Boolean.
  • min_count: int. The default value is 0
    • Add items only when non-NaN values are equal to or more than min_count.

Returns:

  • A scalar value, if the index is not multi-level.
  • A Series, if Index is Multi-index and Level is specified

Let’s understand it with some examples,

Examples of Series.sum()

Get Sum of all values in Pandas Series using Series.sum()

Suppose we have a Pandas Series of numbers and we want to get the sum of all values in this series. Let’s see how to do that,

import pandas as pd
import numpy as np

# Create a Pandas Series from list
series_obj = pd.Series([18, np.NaN, 11, 10, 16, 19])

# Get sum of all values in the Series
total = series_obj.sum()

print('Total: ', total)

Output:

Total:  74.0

Sum() function added all the values in the Series and returned the total. It skipped the NaNs because we didn’t provide the skipna parameter and the default value of skipna is True. But what if we don’t want to skip the NaNs while adding values altogether.

Get Sum of all values in Pandas Series without skipping NaNs

If we add any value in the NaN then it becomes the NaN only. So, to include NaNs while adding value in the Series object, pass the skipna parameter as False in the sum() function,

import pandas as pd
import numpy as np

# Create a Pandas Series from list
series_obj = pd.Series([18, np.NaN, 11, 10, 16, 19])

# Get sum of all values in the Series without Skipping NaNs
total = series_obj.sum(skipna=False)

print('Total: ', total)

Output:

Total:  nan

As our Series object contains the NaN values and we didn’t skip them, therefore the final total is NaN.

Add values in Pandas Series of non-numeric items

If our Series object contains characters instead of numbers, then the sum() function will join these characters and returns a string value i.e.

import pandas as pd
import numpy as np

# Create a Pandas Series from list
series_obj = pd.Series(['a', 'b', 'c', 'd', 'e', 'f'])

# Add values in Series of non-numeric items
total = series_obj.sum()

print('Total: ', total)
print('Type of result: ', type(total))

Output:

Total:  abcdef
Type of result:  <class 'str'>

It concatenated all the characters in the Series object and returned string value.

Pandas Series.sum() & min_count

If we specify the min_count parameter, then sum() function will add the values in Series only if the number of non-NaN items is either greater than or equal to min_count. For example,

import pandas as pd
import numpy as np

# Create a Pandas Series from list
series_obj = pd.Series([18, np.NaN, np.NaN, 10, 16, np.NaN])

# Sum only if non NaN values are greater than or equal to 4
total = series_obj.sum(min_count = 4)

print('Total: ', total)

Output:

Total:  nan

As series object had 3 non NaN values only and we passed min_count as 4, therefore it didn’t added the items in Series and returned NaN.
Now if pass the min_count as 3 then it will add the items i.e.

# Sum only if non NaN values are greater than or equal to 4
total = series_obj.sum(min_count = 3)

print('Total: ', total)

Output:

Total:  44.0

As Series has 3 non NaN items and its equal to min_count therefore if added all these 3 values and returned the total.

Conclusion:

We can use Series.sum() function to get the sum of values in a Pandas Series in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top