Series.sum(self, axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)
[source]
Return the sum of the values for the requested axis.
This is equivalent to the methodnumpy.sum
. Parameters: |
|
---|---|
Returns: |
|
See also
Series.sum
Series.min
Series.max
Series.idxmin
Series.idxmax
DataFrame.sum
DataFrame.min
DataFrame.max
DataFrame.idxmin
DataFrame.idxmax
>>> 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
>>> s.sum() 14
Sum using level names, as well as indices.
>>> s.sum(level='blooded') blooded warm 6 cold 8 Name: legs, dtype: int64
>>> s.sum(level=0) blooded warm 6 cold 8 Name: legs, dtype: int64
By default, the sum of an empty or all-NA Series is 0
.
>>> pd.Series([]).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([]).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
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.Series.sum.html