📅  最后修改于: 2020-10-29 02:06:52             🧑  作者: Mango
mean()函数用于返回所请求轴的值的平均值。如果我们将此方法应用于Series对象,则它将返回标量值,该标量值是数据框中所有观测值的平均值。
如果我们将此方法应用于DataFrame对象,则它将返回Series对象,该对象包含指定轴上的值的平均值。
DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
axis
: {索引(0),列(1)}。这是指要应用的函数的轴。level
:如果轴是MultiIndex(分层),则它将与特定级别一起计数并折叠为一个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
# 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