Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Series.sum() function

Pandas Series.sum() function

Pandas Common Functions Common Pandas functions


Series.sum() is a function in Pandas used to compute the sum of all elements in a Series. It is one of the most commonly used statistical functions in data analysis and can quickly obtain the total of numerical data.

Whether computing total sales, total profit, or total quantity,sum() it helps you get it done quickly. It is especially suited to financial data, statistical reports, sales data analysis, and similar scenarios.


Basic syntax and parameters

sum() is a member function of the Series object and is called directly with the dot operator.

Syntax format



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

Parameter Description

Parameters Type Description Default value
axis int Specifies the axis; a Series has only one axis, so this parameter exists mainly for compatibility with DataFrame. None
skipna bool If True, NaN values are skipped during the calculation; if False, encountering NaN results in NaN. True
level int or str If the Series has a MultiIndex, specifies the level to calculate. None
numeric_only bool If True, only numeric data is calculated; otherwise, conversion to numeric is attempted. False
min_count int The minimum number of valid values required for the calculation. If there are fewer valid values, NaN is returned. 0

Return Value


Example

Let us thoroughly master Series.sum() through a series of examples from simple to complex.

Example 1: Basic Usage - Computing the Sum of a Numerical List

The most basic usage is to create a numeric Series and then call sum() Compute the total.

Example

import

pandas

as

pd

Create a Series containing sales figures

Simulate daily sales data over a week

sales_data

=

pd.

Series

(

[

1200

,

1500

,

1800

,

900

,

2100

,

1600

,

1350

]

)

Compute total sales

total_sales

=

sales_data.

sum

(

)

print

(

"Daily sales:"

)

print

(

sales_data

)

print

(

)

print

(

f

"Total sales: {total_sales}"

)

Running result:



Daily sales:

0    1200

1    1500

2    1800

3     900

4    2100

5    1600

6    1350

dtype: int64



总销售额:10450

Code analysis:

Example 2: Working with data that contains missing values

Real-world data often contains missing values,skipna The parameter determines how these missing values are handled.

Example

import

pandas

as

pd

import

numpy

as

np

Create a Series containing missing values

Simulate missing sales data for some days

sales_with_nan

=

pd.

Series

(

[

1200

,

1500

,

np.

nan

,

1800

,

np.

nan

,

2100

,

1600

]

)

print

(

"Sales data with missing values:"

)

print

(

sales_with_nan

)

print

(

)

By default skipna=True, skipping NaN when computing the sum

total_skipna

=

sales_with_nan.

sum

(

)

print

(

f

"Sum with skipna=True (default): {total_skipna}"

)

Set skipna=False to return NaN when NaN is encountered

total_no_skipna

=

sales_with_nan.

sum

(

skipna

=

False

)

print

(

f

"Sum with skipna=False: {total_no_skipna}"

)

Running result:



Sales data containing missing values:

0    1200.0

1    1500.0

2       NaN

3    1800.0

4       NaN

5    2100.0

6    1600.0

dtype: float64



skipna=True(默认)时的总和:8200.0

skipna=False 时的总和:nan

Code analysis:

Example 3: Using the min_count Parameter

min_count The parameter can set the minimum number of valid values required for the calculation, which is useful when data sufficiency must be guaranteed.

Example

import

pandas

as

pd

import

numpy

as

np

Create a Series that is mostly missing values

sparse_data

=

pd.

Series

(

[

np.

nan

,

np.

nan

,

100

,

np.

nan

,

np.

nan

]

)

print

(

"Mostly missing data:"

)

print

(

sparse_data

)

print

(

)

Default min_count=0; computes as long as there are at least 0 valid values

result_default

=

sparse_data.

sum

(

)

print

(

f

"Result with min_count=0 (default): {result_default}"

)

Set min_count=3; requires at least 3 valid values to compute

result_min_count

=

sparse_data.

sum

(

min_count

=

3

)

print

(

f

"Result with min_count=3: {result_min_count}"

)

Set min_count=2; only 2 valid values are needed

result_min_count_2

=

sparse_data.

sum

(

min_count

=

2

)

print

(

f

"Result with min_count=2: {result_min_count_2}"

)

Running result:



Most missing data:

0     NaN

1     NaN

2    100.0

3     NaN

4     NaN

dtype: float64



min_count=0(默认)时的结果:100.0

min_count=3 时的结果:nan

min_count=2 时的结果:100.0

Code analysis:

Example 4: Application in Real Data Analysis

Combining real business scenarios, this demonstrates sum() a typical application of the function.

Example

import

pandas

as

pd

Create a mock sales data DataFrame

sales_data

=

pd.

DataFrame

(

{

   

'Month'

:

[

'Jan'

,

'Feb'

,

'Mar'

,

'Apr'

,

'May'

,

'Jun'

]

,

   

'East China region'

:

[

12000

,

15000

,

18000

,

16000

,

14000

,

17000

]

,

   

'North China region'

:

[

10000

,

11000

,

13000

,

12500

,

11500

,

14000

]

,

   

'South China region'

:

[

15000

,

17000

,

19000

,

18000

,

16000

,

20000

]

}

)

print

(

"First-half sales data by region:"

)

print

(

sales_data

)

print

(

)

Compute the half-year total sales for each region

for

region

in

[

'East China region'

,

'North China region'

,

'South China region'

]

:

    total

=

sales_data

[

region

]

.

sum

(

)

   

print

(

f

"{region} half-year total sales: {total} yuan"

)

print

(

)

Compute the total sales across all regions

total_all

=

sales_data

[

[

'East China region'

,

'North China region'

,

'South China region'

]

]

.

sum

(

)

.

sum

(

)

print

(

f

"Total half-year sales for all regions: {total_all} yuan"

)

Running result:



Sales data for each region in the first half of the year:

   月份   华东区   华北区   华南区

0  1月  12000  10000  15000

1  2月  15000  11000  17000

2  3月  18000  13000  19000

3  4月  16000  12500  18000

4  5月  14000  11500  16000

5  6月  17000  14000  20000



华东区半年总销售额:92000 元

华北区半年总销售额:72000 元

华南区半年总销售额:105000 元



所有区域半年总销售额:269000 元


Notes


Summary

Series.sum() is one of the most fundamental and commonly used statistical functions in Pandas. Its main features include:

In real data analysis,sum() It is often used together with other aggregation functions (such as mean()、count() , etc.) for comprehensive statistical data analysis.

Pandas Common Functions Common Pandas functions