Sum of series in python

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

Return the sum of the values over the requested axis.

This is equivalent to the method numpy.sum.

Parametersaxis{index (0)}

Axis for the function to be applied on.

skipnabool, default True

Exclude NA/null values when computing the result.

levelint or level name, default None

If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar.

numeric_onlybool, default None

Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.

min_countint, default 0

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.

**kwargs

Additional keyword arguments to be passed to the function.

Returnsscalar or Series (if level specified)

Examples

>>> idx = pd.MultiIndex.from_arrays([
...     ['warm', 'warm', 'cold', 'cold'],
...     ['dog', 'falcon', 'fish', 'spider']],
...     names=['blooded', 'animal'])
>>> s = pd.Series([4, 2, 0, 8], name='legs', index=idx)
>>> s
blooded  animal
warm     dog       4
         falcon    2
cold     fish      0
         spider    8
Name: legs, dtype: int64

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

>>> pd.Series([], dtype="float64").sum()  # min_count=0 is the default
0.0

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.

>>> pd.Series([], dtype="float64").sum(min_count=1)
nan

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

>>> pd.Series([np.nan]).sum()
0.0

>>> pd.Series([np.nan]).sum(min_count=1)
nan

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

    Pandas Series.sum() method is used to get the sum of the values for the requested axis.

    Syntax: Series.sum(axis=None, skipna=None, level=None, numeric_only=None, min_count=0)

    Parameters:
    axis : {index (0)}
    skipna[boolean, default True] : Exclude NA/null values. If an entire row/column is NA, the result will be NA
    level[int or level name, default None] : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar.
    numeric_only[boolean, default None] : Include only float, int, boolean data. If None, will attempt to use everything, then use only numeric data

    Returns: Returns the sum of the values for the requested axis

    Code #1: By default, the sum of an empty or all-NA Series is 0.

    import pandas as pd 

    pd.Series([]).sum()

    pd.Series([]).sum(min_count = 1)

    Output:

    0.0
    nan

     
    Code #2:

    Output:

    2159837111.0

    Code #3:

    import pandas as pd 

    data = {'name': ['John', 'Peter', 'Karl'],

            'age' : [23, 42, 19]}

    val = pd.DataFrame(data)

    val['total'] = val['age'].sum()

    val

    Output:

    Sum of series in python


    How do you sum a Pandas series in Python?

    sum() method is used to get the sum of the values for the requested axis. level[int or level name, default None] : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar.

    How do you add two series in Python?

    concat() method you can combine/merge two or more series into a DataFrame (create DataFrame from multiple series). Besides this you can also use Series..
    Using pandas. concat() to Combine Two Series. ... .
    Combine Two Series Using pandas. merge() ... .
    Combine Two Series Using DataFrame. join() ... .
    Using Series..

    What is sum () in Python?

    Python sum() Function The sum() function returns a number, the sum of all items in an iterable.

    What is sum () in pandas?

    Pandas DataFrame sum() Method The sum() method adds all values in each column and returns the sum for each column. By specifying the column axis ( axis='columns' ), the sum() method searches column-wise and returns the sum of each row.