# Pandas DataFrame DataFrame.median() 函式

Jinku Hu 2023年1月30日

Python Pandas `DataFrame.median()` 函式計算 DataFrame 物件的元素沿指定軸的中位數。

## `pandas.DataFrame.median()` 語法

``````DataFrame.median(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
``````

### 引數

`axis` 沿行(`axis=0`)或列(`axis=1`)找中位數
`skipna` 布林型。排除 `NaN` 值(`skipna=True`)或包含 `NaN` 值(`skipna=False`)
`level` 如果軸為 `MultiIndex`，則沿特定級別尋找中位數
`numeric_only` 布林型。布林型。對於 `numeric_only=True`，只包括 `float``int``boolean`
`**kwargs` 函式的附加關鍵字引數

## 示例程式碼：`DataFrame.median()` 方法沿列軸尋找中位數

``````import pandas as pd

df = pd.DataFrame({'X': [1, 2, 7, 5, 10],
'Y': [4, 3, 8, 2, 9]})
print("DataFrame:")
print(df)

medians=df.median()
print("medians of Each Column:")
print(medians)
``````

``````DataFrame:
X  Y
0   1  4
1   2  3
2   7  8
3   5  2
4  10  9
medians of Each Column:
X    5.0
Y    4.0
dtype: float64
``````

``````import pandas as pd

df = pd.DataFrame({'X': [1, 2, 7, 5, 10],
'Y': [4, 3, 8, 2, 9]})
print("DataFrame:")
print(df)

medians=df["X"].median()
print("medians of Each Column:")
print(medians)
``````

``````DataFrame:
X  Y
0   1  4
1   2  3
2   7  8
3   5  2
4  10  9
medians of Each Column:
5.0
``````

## 示例程式碼：`DataFrame.median()` 方法沿行軸尋找中位數

``````import pandas as pd

df = pd.DataFrame({'X': [1, 2, 7, 5, 10],
'Y': [4, 3, 8, 2, 9],
'Z': [2, 7, 6, 10, 5]})
print("DataFrame:")
print(df)

medians=df.median(axis=1)
print("medians of Each Row:")
print(medians)
``````

``````DataFrame:
X  Y   Z
0   1  4   2
1   2  3   7
2   7  8   6
3   5  2  10
4  10  9   5
medians of Each Row:
0    2.0
1    3.0
2    7.0
3    5.0
4    9.0
dtype: float64
``````

``````import pandas as pd

df = pd.DataFrame({'X': [1, 2, 7, 5, 10],
'Y': [4, 3, 8, 2, 9],
'Z': [2, 7, 6, 10, 5]})

print("DataFrame:")
print(df)

median=df.iloc[[0]].median(axis=1)
print("median of 1st Row:")
print(median)
``````

``````DataFrame:
X  Y   Z
0   1  4   2
1   2  3   7
2   7  8   6
3   5  2  10
4  10  9   5
median of 1st Row:
0    2.0
dtype: float64
``````

## 示例程式碼：`DataFrame.median()` 方法忽略 `NaN` 值來尋找中位數

``````import pandas as pd

df = pd.DataFrame({'X': [1, 2, 7, None, 10, 8],
'Y': [None, 3, 8, 2, 9, 6],
'Z': [2, 7, 6, 10, None, 5]})

print("DataFrame:")
print(df)

median=df.median(skipna=True)
print("medians of Each Row:")
print(median)
``````

``````DataFrame:
X    Y     Z
0   1.0  NaN   2.0
1   2.0  3.0   7.0
2   7.0  8.0   6.0
3   NaN  2.0  10.0
4  10.0  9.0   NaN
5   8.0  6.0   5.0
medians of Each Row:
X    7.0
Y    6.0
Z    6.0
dtype: float64
``````

``````import pandas as pd

df = pd.DataFrame({'X': [1, 2, 7, None, 10],
'Y': [5, 3, 8, 2, 9],
'Z': [2, 7, 6, 10, 4]})

print("DataFrame:")
print(df)

median=df.median(skipna=False)
print("medians of Each Row:")
print(median)
``````

``````DataFrame:
X  Y   Z
0   1.0  5   2
1   2.0  3   7
2   7.0  8   6
3   NaN  2  10
4  10.0  9   4
medians of Each Row:
X    NaN
Y    5.0
Z    6.0
dtype: float64
``````

DelftStack.com 創辦人。Jinku 在機器人和汽車行業工作了8多年。他在自動測試、遠端測試及從耐久性測試中創建報告時磨練了自己的程式設計技能。他擁有電氣/ 電子工程背景，但他也擴展了自己的興趣到嵌入式電子、嵌入式程式設計以及前端和後端程式設計。