python dataframe mean_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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值