pandas dataframe dataframe.mean() 函数-ag捕鱼王app官网

pandas dataframe dataframe.mean() 函数

作者:迹忆客 最近更新:2024/04/22 浏览次数:

python pandas dataframe.mean() 函数计算 dataframe 对象在指定轴上的值的平均值。


pandas.dataframe.mean() 语法

dataframe.mean(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,只包括 floatintboolean
**kwargs 函数的附加关键字参数

返回值

如果没有指定 level,则返回请求轴的平均值的 series,否则返回平均值的 dataframe


示例代码:dataframe.mean() 方法沿列轴寻找平均值

import pandas as pd
df = pd.dataframe({'x': [1, 2, 2, 3],
                   'y': [4, 3, 8, 4]})
print("dataframe:")
print(df)
means=df.mean()
print("means of each column:")
print(means)

输出:

dataframe:
   x  y
0  1  4
1  2  3
2  2  8
3  3  4
means of each column:
x    2.00
y    4.75
dtype: float64

计算 xy 两列的平均数,最后返回一个包含每列平均数的 series 对象。

在 pandas 中,如果要找到 dataframe 中某一列的平均数,我们只调用该列的 mean() 函数。

import pandas as pd
df = pd.dataframe({'x': [1, 2, 2, 3],
                   'y': [4, 3, 8, 4]})
print("dataframe:")
print(df)
means=df["x"].mean()
print("mean of column x:")
print(means)

输出:

dataframe:
   x  y
0  1  4
1  2  3
2  2  8
3  3  4
mean of column x:
2.0

它只给出 dataframex 列数值的平均值。


示例代码: dataframe.mean() 方法沿行轴寻找平均值

import pandas as pd
df = pd.dataframe({'x': [1, 2, 2, 3],
                   'y': [4, 3, 8, 4]})
print("dataframe:")
print(df)
means=df.mean(axis=1)
print("mean of rows:")
print(means)

输出:

dataframe:
   x  y
0  1  4
1  2  3
2  2  8
3  3  4
mean of rows:
0    2.5
1    2.5
2    5.0
3    3.5
dtype: float64

它计算所有行的平均值,最后返回一个包含每行平均值的 series 对象。

在 pandas 中,如果要找到 dataframe 中某一行的均值,我们只调用 mean() 函数来计算这一行的均值。

import pandas as pd
df = pd.dataframe({'x': [1, 2, 2, 3],
                   'y': [4, 3, 8, 4]})
print("dataframe:")
print(df)
mean=df.iloc[[0]].mean(axis=1)
print("mean of 1st row:")
print(mean)

输出:

dataframe:
   x  y
0  1  4
1  2  3
2  2  8
3  3  4
mean of 1st row:
0    2.5
dtype: float64

它只给出 dataframe 中第一行数值的平均值。

我们使用 iloc 方法根据索引选择行。


示例代码:dataframe.mean() 方法忽略 nan 值来寻找平均值

我们使用 skipna 参数的默认值,即 skipna=true 来寻找 dataframe 沿指定轴的平均值,忽略 nan 值。

import pandas as pd
df = pd.dataframe({'x': [1, 2, none, 3],
                   'y': [4, 3, none, 4]})
print("dataframe:")
print(df)
means=df.mean(skipna=true)
print("mean of columns")
print(means)

输出:

dataframe:
     x    y
0  1.0  4.0
1  2.0  3.0
2  nan  nan
3  3.0  4.0
mean of columns
x    2.000000
y    3.666667
dtype: float64

如果我们设置 skipna=true,它将忽略 dataframe 中的 nan。它允许我们沿列轴计算 dataframe 的平均值,忽略 nan 值。

import pandas as pd
df = pd.dataframe({'x': [1, 2, none, 3],
                   'y': [4, 3, 3, 4]})
print("dataframe:")
print(df)
means=df.mean(skipna=false)
print("mean of columns")
print(means)

输出:

dataframe:
     x  y
0  1.0  4
1  2.0  3
2  nan  3
3  3.0  4
mean of columns
x    nan
y    3.5
dtype: float64

在这里,我们得到了列 x 的平均值的 nan 值,因为列 x 中存在 nan 值。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图