mean python

Hey, folks! In this article, we will be focusing on Python mean() function in order to perform the mean operation.

嘿伙计! 在本文中,我们将专注于Python mean()函数以执行均值运算。

Mean is the value that represents an entire set of entities. It is considered as the central value of a set of numbers.

平均值是代表整个实体集的值。 它被视为一组数字的中心值

Mean is calculated by dividing the summation of all the entity values by the number of entities. Essentially, the mean is the average of the specified values.

通过将所有实体值的总和除以实体数来计算平均值。 本质上,平均值是指定值的平均值。

Formula:

式:

(sum of values)/total values

(sum of values)/total values

Now, let us understand the working of the mean() function to calculate the mean.

现在,让我们了解一下mean()函数计算平均值的工作原理。



使用Python mean()函数 (Using the Python mean() function)

The mean() function helps in calculating the mean of a set of values passed to the function.

mean()函数有助于计算传递给该函数的一组值的平均值。

Python statistics module is used to perform all the statistical operations on the data. We need to import the statistics module using the below command:

Python统计模块用于对数据执行所有统计操作。 我们需要使用以下命令导入统计信息模块:

Syntax: Importing the statistics module

语法 :导入统计信息模块


import statistics

The statistics.mean() function accepts the data values as argument and returns the mean of the values passed to it.

statistics.mean()函数接受数据值作为参数,并返回传递给它的值的平均值。

Syntax:

句法:


statistics.mean(data)

Example:

例:


import statistics
data = [10,20,30,40,50]

res_mean = statistics.mean(data)
print(res_mean)

Output:

输出:


30


带有NumPy模块的Python mean()函数 (Python mean() function with NumPy module)

Python NumPy module represents the set of values in the form of an array. We can calculate the mean of these array elements using numpy.mean() function.

Python NumPy模块以数组形式表示一组值。 我们可以使用numpy.mean()函数计算这些数组元素的平均值。

The numpy.mean() function performs the same way as of the statistics.mean() function.

numpy.mean() function执行方式与statistics.mean()函数相同。

Syntax:

句法:


numpy.mean(data)

Example:

例:


import numpy as np
data = np.arange(1,10)
res_mean = np.mean(data)
print(res_mean)

In the above example, we have used the numpy.arange(start,stop) function to generate evenly spaced values in the range provided as parameters. Further, numpy.mean() function is used to calculate the mean of all the array elements.

在上面的示例中,我们使用了numpy.arange(start,stop)函数在作为参数提供的范围内生成均匀间隔的值。 此外, numpy.mean() function用于计算所有数组元素的均值。

Output:

输出:


5.0


带有Pandas模块的Python mean()函数 (Python mean() function with Pandas module)

Python Pandas module deals with huge datasets in the form of DataFrames. The mean of those huge data sets can be calculated by using pandas.DataFrame.mean() function.

Python Pandas模块以DataFrames的形式处理庞大的数据集。 可以使用pandas.DataFrame.mean()函数计算这些庞大数据集的平均值。

The pandas.DataFrame.mean() function returns the mean of those data values.

pandas.DataFrame.mean()函数返回这些数据值的平均值。

Syntax:

句法:


pandas.DataFrame.mean()

Example 1:

范例1:


import numpy as np
import pandas as pd
data = np.arange(1,10)
df = pd.DataFrame(data)
res_mean = df.mean()
print(res_mean)

In the above example, we have created a NumPy array using numpy.arange() function and then converted the array values into a DataFrame using pandas.DataFrame() function. Further, we have calculated the mean of the DataFrame values using pandas.DataFrame.mean() function.

在上述例子中,我们已经使用创建的NumPy的阵列 numpy.arange() function ,然后使用变换后的数组值成数据帧pandas.DataFrame() function 。 此外,我们使用pandas.DataFrame.mean() function计算了DataFrame值的平均值。

Output:

输出:


0    5.0
dtype: float64

Example 2:

范例2:


import pandas as pd 
data = pd.read_csv("C:/mtcars.csv")
res_mean = data['qsec'].mean()
print(res_mean)

Input Dataset:

输入数据集:

MTCARS Dataset
MTCARS Dataset MTCARS数据集

In the above example, we have used the above mentioned dataset, and calculated the mean of all the data values present in the data column ‘qsec‘.

在上面的示例中,我们使用了上面提到的数据集,并计算了数据列“ qsec ”中所有数据值的平均值

Output:

输出:


17.848750000000003


结论 (Conclusion)

Thus, in this article, we have understood the working of Python mean() function along with NumPy and Pandas module.

因此,在本文中,我们了解了Python mean()函数以及NumPy和Pandas模块的工作方式。



参考资料 (References)

Python mean() function — Official Documentation

Python mean()函数—官方文档

翻译自: https://www.journaldev.com/39570/python-mean-method

mean python

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐