Return the sum of the values over the requested axis.
This is equivalent to the method
numpy.sum
.
Axis for the function to be applied on.
skipnabool, default TrueExclude NA/null values when computing the result.
levelint or level name, default NoneIf the axis is a MultiIndex [hierarchical], count along a particular level, collapsing into a scalar.
numeric_onlybool, default NoneInclude only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
min_countint, default 0The required number of valid values to perform the operation. If fewer than min_count
non-NA values are present the result will be NA.
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
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 dataReturns: 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: