Pandas DataFrame.median(~) 方法计算 DataFrame 的每行或每列的中位数。
参数
1.axis | int 或 string | optional
是否按行或按列计算中位数:
|
轴 |
说明 |
|---|---|
|
|
计算每列的中位数。 |
|
|
计算每行的中位数。 |
默认情况下,axis=0 。
2. skipna | boolean | optional
是否跳过 NaN 。如果 skipna=False ,那么即使有一个 NaN 也会返回 NaN 作为其行/列的中位数。默认情况下,skipna=True 。
3. level | string 或 int | optional
要考虑的级别的名称或整数索引。仅当您的 DataFrame 是多索引时,这才有意义。
4. numeric_only | None 或 boolean | optional
允许的值如下:
|
值 |
说明 |
|---|---|
|
|
仅考虑数字行/列(例如 |
|
|
尝试使用所有类型(例如字符串和日期)进行计算,并在无法计算中位数时抛出错误。 |
|
|
尝试计算所有类型,并忽略所有无法计算中位数的行/列不会引发错误. |
请注意,只有当我们可以在类型之间执行求和时,才能计算中位数。
默认情况下,numeric_only=None 。
返回值
如果指定了level参数,则将返回DataFrame。否则,将返回Series。
例子
考虑以下 DataFrame :
df = pd.DataFrame({"A":[2,3], "B":[4,5], "C":["6",7]})
df
A B C
0 2 4 "6"
1 3 5 7
列中位数
计算每列的中位数:
df.median() # axis=0
A 2.5
B 4.5
C 6.5
dtype: float64
请注意 "6" 如何自动转换为 float 以计算中位数。
行中位数
要计算每行的中位数,请设置 axis=1 :
df.median(axis=1)
0 4.0
1 5.0
dtype: float64
指定skipna
考虑以下 DataFrame :
df = pd.DataFrame({"A":[3,4,6], "B":[7,9,pd.np.nan]})
df
A B
0 3 7.0
1 4 9.0
2 6 NaN
默认情况下, skipna=True ,这意味着在计算中位数时会跳过所有缺失值:
df.median() # skipna=True
A 4.5
B 8.0
dtype: float64
考虑缺失值:
df.median(skipna=False)
A 4.0
B NaN
dtype: float64
请注意,如果行/列包含一个或多个缺失值,则该行/列的中位数将为 NaN 。
指定numeric_only
考虑以下 DataFrame :
df = pd.DataFrame({"A":[3,4], "B":[5,True], "C":[6,"7@8"]})
df
A B C
0 3 5 6
1 4 True 7@8
此处, B 和 C 列都包含混合类型,但主要区别在于可以计算 B 的中位数,但不能计算 C 的中位数。当样本大小为偶数时(即此处的情况),中位数是通过取中间两个数字的平均值来计算的,这意味着类型之间的求和运算必须明确定义。
回想一下, True 布尔值的内部表示是 1 ,因此操作 5+True 实际上计算为 6 :
5 + True
6
另一方面,6+"7@8" 抛出错误:
6 + "7@8"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
None
默认情况下, numeric_only=None ,这意味着混合类型的行/列也会被考虑:
df.median(numeric_only=None)
A 3.5
B 3.0
dtype: float64
在这里,请注意如何计算列的中位数B,但不适合C。通过传入None,无法计算中位数的行/列(由于求和无效)将被忽略不会引发错误.
False
通过设置 numeric_only=False ,将再次考虑混合类型的行/列,但当无法计算中位数时会抛出错误:
df.median(numeric_only=False)
TypeError: could not convert string to float: '7@8'
在这里,我们最终得到一个错误,因为 C 列包含未定义求和的混合类型。
True
通过设置 numeric_only=True ,仅考虑数字行/列:
df.median(numeric_only=True)
A 4.5
dtype: float64
请注意B 和C 列如何被忽略,因为它们包含混合类型。
相关用法
- Python Pandas DataFrame mean方法用法及代码示例
- Python Pandas DataFrame memory_usage方法用法及代码示例
- Python Pandas DataFrame merge方法用法及代码示例
- Python Pandas DataFrame melt方法用法及代码示例
- Python Pandas DataFrame max方法用法及代码示例
- Python Pandas DataFrame mod方法用法及代码示例
- Python Pandas DataFrame mode方法用法及代码示例
- Python Pandas DataFrame mask方法用法及代码示例
- Python Pandas DataFrame min方法用法及代码示例
- Python Pandas DataFrame mad方法用法及代码示例
- Python Pandas DataFrame mul方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
- Python Pandas DataFrame swaplevel方法用法及代码示例
- Python Pandas DataFrame agg方法用法及代码示例
- Python Pandas DataFrame copy方法用法及代码示例
- Python Pandas DataFrame pow方法用法及代码示例
- Python Pandas DataFrame insert方法用法及代码示例
- Python Pandas DataFrame lt方法用法及代码示例
- Python Pandas DataFrame all方法用法及代码示例
- Python Pandas DataFrame unstack方法用法及代码示例
- Python PySpark DataFrame filter方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | median method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
