Σ
SDCalc
中級教學指南·15 min

在 Excel 和 Python 中計算標準差

逐步教學如何在 Excel(STDEV.S、STDEV.P)和 Python(numpy、pandas、statistics)中計算標準差,附完整程式碼範例。

Excel:概述

Microsoft Excel 提供了內建函數來計算樣本標準差和母體標準差。這些函數在所有現代版本的 Excel 中都可以使用。

Excel 函數

函數類型說明
`STDEV.S()`樣本樣本標準差(除以 n-1)
`STDEV.P()`母體母體標準差(除以 N)
`STDEV()`樣本舊版函數,等同於 STDEV.S
`STDEVP()`母體舊版函數,等同於 STDEV.P

Excel 範例

Excel Formulas
// Data in cells A1:A10
=STDEV.S(A1:A10)     // Sample SD
=STDEV.P(A1:A10)     // Population SD

// For specific values
=STDEV.S(4, 8, 6, 5, 3)    // Returns 1.924

// Ignoring text and logical values
=STDEV.S(A1:A10)    // Ignores text
=STDEVA(A1:A10)     // Includes text as 0

小提示

大多數實際分析場景中使用 STDEV.S 即可。只有當你確定擁有完整母體資料時,才使用 STDEV.P。

Python:概述

Python 提供多種計算標準差的方式。最常用的函式庫是 NumPyPandas 以及內建的 statistics 模組。

使用 NumPy

Python (NumPy)
import numpy as np

data = [4, 8, 6, 5, 3]

# Population standard deviation (default)
pop_sd = np.std(data)
print(f"Population SD: {pop_sd}")  # 1.720

# Sample standard deviation
sample_sd = np.std(data, ddof=1)
print(f"Sample SD: {sample_sd}")  # 1.924

什麼是 ddof?

ddof 代表“Delta Degrees of Freedom”(自由度差值)。設定 ddof=1 會讓 NumPy 除以 (n-1) 來計算樣本標準差。預設 ddof=0 則計算母體標準差。

使用 Pandas

Python (Pandas)
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'scores': [85, 90, 78, 92, 88]})

# Sample SD (default in pandas)
sample_sd = df['scores'].std()
print(f"Sample SD: {sample_sd}")

# Population SD
pop_sd = df['scores'].std(ddof=0)
print(f"Population SD: {pop_sd}")

# Multiple columns at once
df.std()  # Returns SD for all numeric columns

快速比較

工具樣本標準差母體標準差
Excel`STDEV.S()``STDEV.P()`
NumPy`np.std(data, ddof=1)``np.std(data)`
Pandas`df.std()``df.std(ddof=0)`
Python statistics`stdev()``pstdev()`