pandas dataframe dataframe.median() 函数
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 |
函数的附加关键字参数 |
返回值
如果没有指定 level
,则返回请求轴的中值的 series
,否则返回中位数的 dataframe
。
示例代码: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
计算 x
和 y
两列的中位数,最后返回一个包含每列中位数的 series
对象。
在 pandas 中,要找到 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["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
中的 x
列的中位数。
示例代码: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
它计算所有行的中位数,最后返回一个包含每行中位数的 series
对象。
在 pandas 中,要想找到 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)
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
中第一行数值的中位数。
我们使用 iloc
方法根据索引选择行。
示例代码:dataframe.median()
方法忽略 nan
值来寻找中位数
我们使用 skipna
参数的默认值,即 skipna=true
,通过忽略 nan
值,沿指定的轴找到 dataframe
的中位数。
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
如果我们设置 skipna=true
,它会忽略 dataframe 中的 nan
。它允许我们通过忽略 nan
值来计算 dataframe
沿列轴的中位数。
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
这里,我们得到的是 nan
值作为 x
列的中位数,因为 x
列中存在 nan
值。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串