Python技术迷

Pandas 教程-Pandas DataFrame.mean()

整理:python架构师

mean()函数用于返回所请求轴的值的平均值。如果我们在Series对象上应用此方法,则返回一个标量值,该值是数据框中所有观测值的平均值。

如果我们在DataFrame对象上应用此方法,则返回一个包含指定轴上的值的平均值的Series对象。

语法

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

参数

  • axis: {index (0), columns (1)}。这是应用函数的轴。

  • skipna: 计算结果时,排除所有null值。

  • level: 沿着特定级别进行计数,并且如果轴是MultiIndex(分层结构),则折叠为Series。

  • numeric_only: 只包括int、float、boolean列。如果为None,它将尝试使用所有内容,然后仅使用数值数据。未在Series中实现。

专属福利 
👉点击领取:最全Python资料合集

返回

如果指定了级别,则返回Series或DataFrame的平均值。

示例

# importing pandas as pd   import pandas as pd    # Creating the dataframe    info = pd.DataFrame({"A":[8, 2, 7, 12, 6],                      "B":[26, 19, 7, 5, 9],                       "C":[10, 11, 15, 4, 3],                      "D":[16, 24, 14, 22, 1]})     # Print the dataframe   info  # If axis = 0 is not specified, then  # by default method return the mean over   # the index axis   info.mean(axis = 0)
输出
A     7.0B    13.2C     8.6D    15.4dtype: float64

示例2

# importing pandas as pd   import pandas as pd   # Creating the dataframe    info = pd.DataFrame({"A":[5, 2, 6, 4, None],                      "B":[12, 19, None, 8, 21],                      "C":[15, 26, 11, None, 3],                     "D":[14, 17, 29, 16, 23]})     # while finding mean, it skip null values   info.mean(axis = 1, skipna = True)
输出
0       11.5000001       16.0000002       15.3333333        9.3333334       15.666667dtype: float64
Image
 1
Image
热门推荐
Image