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

Pandas Series.std() Function

Pandas Series.std() Function

Pandas Common Functions Common Pandas functions


Series.std() is a Pandas function used to calculate the standard deviation of a Series. Standard deviation is a measure of data dispersion, indicating the average deviation of data from its mean.

A larger standard deviation means the data is more spread out; a smaller one means the data is more concentrated. It is often used in quality control, risk assessment, exam score analysis, and similar scenarios.


Basic syntax and parameters

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

Syntax format



Series.std(axis=None, skipna=True, level=None, numeric_only=None, ddof=1, **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
ddof int Degrees-of-freedom adjustment parameter. ddof=1 uses the sample standard deviation (n-1); ddof=0 uses the population standard deviation (n). 1

Return Value


Example

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

Example 1: Basic Usage - Understanding the Concept of Standard Deviation

Standard deviation measures how spread out data is; a larger value means the data is more dispersed.

Example

import

pandas

as

pd

import

numpy

as

np

Two sets of score data

Group A: scores are relatively concentrated

group_a

=

pd.

Series

(

[

85

,

86

,

87

,

88

,

89

]

)

Group B: scores are relatively dispersed

group_b

=

pd.

Series

(

[

70

,

75

,

85

,

95

,

100

]

)

print

(

"Group A Scores (More Concentrated):"

)

print

(

group_a

)

print

(

f

"Mean: {group_a.mean():.2f}"

)

print

(

f

"Standard Deviation: {group_a.std():.2f}"

)

print

(

)

print

(

"Group B Scores (More Dispersed):"

)

print

(

group_b

)

print

(

f

"Mean: {group_b.mean():.2f}"

)

print

(

f

"Standard Deviation: {group_b.std():.2f}"

)

print

(

)

print

(

"Analysis: although both groups have the same mean (85), Group B has a larger standard deviation, indicating greater variation in scores."

)

Running result:



A组成绩(较集中):

0    85

1    86

2    87

3    88

4    89

dtype: int64

平均值:85.00

标准差:1.58



B组成绩(较分散):

0    70

1    75

2    85

3    95

4   100

dtype: int64

平均值:85.00

标准差:12.50



分析:虽然两组平均值相同(85),但 B 组标准差更大,说明成绩差异更大。

Code analysis:

Example 2: The Role of the ddof Parameter

ddof The parameter controls whether the sample or population standard deviation is used.

Example

import

pandas

as

pd

import

numpy

as

np

Create a dataset

data

=

pd.

Series

(

[

2

,

4

,

4

,

4

,

5

,

5

,

7

,

9

]

)

print

(

"Data:"

)

print

(

data

)

print

(

)

The default ddof=1 uses the sample standard deviation (division by n-1)

sample_std

=

data.

std

(

ddof

=

1

)

print

(

f

"Sample Standard Deviation (ddof=1): {sample_std:.4f}"

)

ddof=0 uses the population standard deviation (division by n)

population_std

=

data.

std

(

ddof

=

0

)

print

(

f

"Population Standard Deviation (ddof=0): {population_std:.4f}"

)

print

(

)

print

(

"Explanation:"

)

print

(

"Sample Standard Deviation = sqrt(sum((x-mean)^2) / (n-1))"

)

print

(

"Population Standard Deviation = sqrt(sum((x-mean)^2) / n)"

)

print

(

"When the dataset is large, the difference between the two is very small."

)

Running result:



数据:

0    2

1    4

2    4

3    4

4    5

5    5

6    7

7    9

dtype: int64



样本标准差(ddof=1):2.2678

总体标准差(ddof=0):2.1213

Code analysis:

Example 3: Handling Data with Missing Values

Example

import

pandas

as

pd

import

numpy

as

np

Create a Series containing missing values

data_with_nan

=

pd.

Series

(

[

10

,

20

,

np.

nan

,

30

,

40

,

np.

nan

,

50

]

)

print

(

"Data containing missing values:"

)

print

(

data_with_nan

)

print

(

)

By default, skipna=True, which skips NaN values when calculating the standard deviation

std_skipna

=

data_with_nan.

std

(

)

print

(

f

"Standard Deviation with skipna=True (default): {std_skipna:.4f}"

)

Set skipna=False

std_no_skipna

=

data_with_nan.

std

(

skipna

=

False

)

print

(

f

"Standard Deviation with skipna=False: {std_no_skipna}"

)

Running result:



Data containing missing values:

0    10.0

1    20.0

2       NaN

3    30.0

4    40.0

5       NaN

6    50.0

dtype: float64



skipna=True(默认)时的标准差:15.8114

skipna=False 时的标准差:nan

Example 4: Practical Application - Stock Return Volatility Analysis

Standard deviation is often used in finance to measure risk.

Example

import

pandas

as

pd

Simulate the daily returns (%) of two stocks over 10 days

stock_a

=

pd.

Series

(

[

1.2

,

0.8

,

0.5

,

1.5

,

0.3

,

0.2

,

1.0

,

0.7

,

0.3

,

0.5

]

)

stock_b

=

pd.

Series

(

[

3.5

,

2.0

,

4.2

,

1.5

,

2.8

,

3.0

,

1.2

,

0.8

,

3.0

,

2.4

]

)

print

(

"Daily Returns of Stock A (%):"

)

print

(

stock_a

)

print

(

f

"Average Return: {stock_a.mean():.2f}%"

)

print

(

f

"Volatility (Standard Deviation): {stock_a.std():.2f}%"

)

print

(

)

print

(

"Daily Returns of Stock B (%):"

)

print

(

stock_b

)

print

(

f

"Average Return: {stock_b.mean():.2f}%"

)

print

(

f

"Volatility (Standard Deviation): {stock_b.std():.2f}%"

)

print

(

)

print

(

"Analysis:"

)

print

(

"Stock B has a larger standard deviation, indicating more volatile returns and higher risk."

)

print

(

"Although the two may have similar average returns, their risk levels differ."

)

Running result:



股票 A 日收益率(%):

0    1.2

1    0.8

2   -0.5

3    1.5

4    0.3

5   -0.2

6    1.0

7    0.7

8   -0.3

9    0.5

dtype: int64

平均收益率:0.60%

波动率(标准差):0.68%



股票 B 日收益率(%):

0    3.5

1   -2.0

2    4.2

3   -1.5

4    2.8

5   -3.0

6    1.2

7   -0.8

8    3.0

9   -2.4

dtype: int64

平均收益率:0.60%

波动率(标准差):2.59%



分析:股票 B 的波动率是股票 A 的约 4 倍,说明风险高得多。


Notes


Summary

Series.std() is an important function for measuring the dispersion of data. Its main features include:

In real data analysis, standard deviation is often used together with the mean, median, and other measures to form a complete understanding of the data distribution.

Pandas Common Functions Common Pandas functions