Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['cat', 'eagle', 'snake', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 2, 0, 8], name='legs', index=idx)
s
Out[2]:
blooded  animal
warm     cat       4
         eagle     2
cold     snake     0
         spider    8
Name: legs, dtype: int64

Pandas: Dataframe - sum.

In [3]:
s.sum()
Out[3]:
14

Pandas: Dataframe - sum.

Sum using level names, as well as indices:

In [4]:
s.sum(level='blooded')
Out[4]:
blooded
warm    6
cold    8
Name: legs, dtype: int64

Pandas: Dataframe - Sum using level names, as well as indices.

In [5]:
s.sum(level=0)
Out[5]:
blooded
warm    6
cold    8
Name: legs, dtype: int64

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

In [6]:
pd.Series([]).sum()  # min_count=0 is the default
Out[6]:
0.0
In [7]:
pd.Series([]).sum(min_count=1)
Out[7]:
nan
In [8]:
pd.Series([np.nan]).sum()
Out[8]:
0.0
In [9]:
pd.Series([np.nan]).sum(min_count=1)
Out[9]:
nan