个性化阅读
专注于IT技术分析

Pandas DataFrame.mean()例子

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

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

句法

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

参数

  • 轴:{索引(0), 列(1)}。
    这是指要应用的功能的轴。
  • skipna:计算结果时, 它排除所有空值。
  • 级别:如果轴是MultiIndex(分层), 则它将与特定级别一起计数并折叠为一个Series,
  • numeric_only:仅包含int, float和boolean列。如果为None, 它将尝试使用所有内容, 然后仅使用数字数据。未针对系列实施。

退货

如果指定了级别, 则返回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

赞(0)
未经允许不得转载:srcmini » Pandas DataFrame.mean()例子

评论 抢沙发

评论前必须登录!