Pandas教程-Pandas DataFrame.mean()

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中实现。
返回
如果指定了级别,则返回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.0
B 13.2
C 8.6
D 15.4
dtype: 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.500000
1 16.000000
2 15.333333
3 9.333333
4 15.666667
dtype: float64