How do you sum a pandas 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:

    How do you sum a pandas series in python?


    How do you sum up the pandas series?

    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 I sum a column in pandas Python?

    sum() to Sum All Columns. Use DataFrame. sum() to get sum/total of a DataFrame for both rows and columns, to get the total sum of columns use axis=1 param. By default, this method takes axis=0 which means summing of rows.

    How do you sum a list in Python?

    sum() function in Python Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

    How do you add two series in Python?

    The Series. add() method not only adds elements from two pandas. Series instances, it also adds elements from any Python sequence such as list with the elements of a pandas. Series instance.