Pandas Series.std() Function
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
- Return Type:
float - Description: Returns the standard deviation of the elements in the Series. By default, the sample standard deviation is used (dividing by n-1).
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
]
)
(
"Group A Scores (More Concentrated):"
)
(
group_a
)
(
f
"Mean: {group_a.mean():.2f}"
)
(
f
"Standard Deviation: {group_a.std():.2f}"
)
(
)
(
"Group B Scores (More Dispersed):"
)
(
group_b
)
(
f
"Mean: {group_b.mean():.2f}"
)
(
f
"Standard Deviation: {group_b.std():.2f}"
)
(
)
(
"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:
- Group A has a standard deviation of about 1.58, which is very concentrated.
- Group B has a standard deviation of about 12.50, which is far more dispersed.
- This shows that even with the same mean, the shape of the data distribution can be completely different.
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
]
)
(
"Data:"
)
(
data
)
(
)
The default ddof=1 uses the sample standard deviation (division by n-1)
sample_std
=
data.
std
(
ddof
=
1
)
(
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
)
(
f
"Population Standard Deviation (ddof=0): {population_std:.4f}"
)
(
)
(
"Explanation:"
)
(
"Sample Standard Deviation = sqrt(sum((x-mean)^2) / (n-1))"
)
(
"Population Standard Deviation = sqrt(sum((x-mean)^2) / n)"
)
(
"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:
- The sample standard deviation (ddof=1) uses n-1 as the divisor and applies to samples drawn from a population.
- The population standard deviation (ddof=0) uses n as the divisor and applies to the entire dataset.
- Pandas uses ddof=1 by default, meaning the sample standard deviation.
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
]
)
(
"Data containing missing values:"
)
(
data_with_nan
)
(
)
By default, skipna=True, which skips NaN values when calculating the standard deviation
std_skipna
=
data_with_nan.
std
(
)
(
f
"Standard Deviation with skipna=True (default): {std_skipna:.4f}"
)
Set skipna=False
std_no_skipna
=
data_with_nan.
std
(
skipna
=
False
)
(
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
]
)
(
"Daily Returns of Stock A (%):"
)
(
stock_a
)
(
f
"Average Return: {stock_a.mean():.2f}%"
)
(
f
"Volatility (Standard Deviation): {stock_a.std():.2f}%"
)
(
)
(
"Daily Returns of Stock B (%):"
)
(
stock_b
)
(
f
"Average Return: {stock_b.mean():.2f}%"
)
(
f
"Volatility (Standard Deviation): {stock_b.std():.2f}%"
)
(
)
(
"Analysis:"
)
(
"Stock B has a larger standard deviation, indicating more volatile returns and higher risk."
)
(
"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
- The sample standard deviation (ddof=1) is used by default, which is suitable for statistical analysis.
- If you need the population standard deviation, set ddof=0.
- The units of standard deviation are the same as those of the original data, making it easy to interpret.
- A larger standard deviation indicates more dispersed data; a smaller one indicates more concentrated data.
Summary
Series.std() is an important function for measuring the dispersion of data. Its main features include:
- Uses the sample standard deviation (ddof=1) by default, which is more suitable for statistical analysis.
- It can be used together with
mean()When used together with the mean, it fully describes the characteristics of the data distribution. - In finance, standard deviation is often used to measure risk (volatility).
- The square of the standard deviation is the variance (
var())。
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.
Common Pandas functions