>>> from env_helper import info; info()
页面更新时间: 2023-12-16 21:42:44
运行环境:
    Linux发行版本: Debian GNU/Linux 12 (bookworm)
    操作系统内核: Linux-6.1.0-15-amd64-x86_64-with-glibc2.36
    Python版本: 3.11.2

6.6. Pandas描述性统计

有很多方法用来集体计算DataFrame的描述性统计信息和其他相关操作。 其中大多数是sum(),mean()等聚合函数,但其中一些,如sumsum(),产生一个相同大小的对象。 一般来说,这些方法采用轴参数,就像ndarray.{sum,std,…},但轴可以通过名称或整数来指定:

数据帧(DataFrame) - “index”(axis=0,默认),columns(axis=1)

下面创建一个数据帧(DataFrame),并使用此对象进行演示本章中所有操作。

6.6.1. 示例

>>> import pandas as pd
>>> import numpy as np

Create a Dictionary of series

>>> d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack',
>>>    'Lee','David','Gasper','Betina','Andres']),
>>>    'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]),
>>>    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])}

Create a DataFrame

>>> df = pd.DataFrame(d)
>>> df
Name Age Rating
0 Tom 25 4.23
1 James 26 3.24
2 Ricky 25 3.98
3 Vin 23 2.56
4 Steve 30 3.20
5 Minsu 29 4.60
6 Jack 23 3.80
7 Lee 34 3.78
8 David 40 2.98
9 Gasper 30 4.80
10 Betina 51 4.10
11 Andres 46 3.65

sum() 方法

返回所请求轴的值的总和。 默认情况下,轴为索引( axis=0 )。

>>> df.sum()
Name      TomJamesRickyVinSteveMinsuJackLeeDavidGasperBe...
Age                                                     382
Rating                                                44.92
dtype: object

每个单独的列单独添加(附加字符串)。

axis=1示例

此语法将给出如下所示的输出,参考以下示例代码 -

>>> df.sum(1)
/tmp/ipykernel_21191/681064937.py:1: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.
  df.sum(1)
0     29.23
1     29.24
2     28.98
3     25.56
4     33.20
5     33.60
6     26.80
7     37.78
8     42.98
9     34.80
10    55.10
11    49.65
dtype: float64

mean() 示例

返回平均值,参考以下示例代码。

查看结果:

>>> df.mean()
/tmp/ipykernel_21191/3698961737.py:1: FutureWarning: The default value of numeric_only in DataFrame.mean is deprecated. In a future version, it will default to False. In addition, specifying 'numeric_only=None' is deprecated. Select only valid columns or specify the value of numeric_only to silence this warning.
  df.mean()
Age       31.833333
Rating     3.743333
dtype: float64

std() 示例

返回数字列的Bressel标准偏差。

>>> print(df.std())
Age       9.232682
Rating    0.661628
dtype: float64
/tmp/ipykernel_21191/2597523637.py:1: FutureWarning: The default value of numeric_only in DataFrame.std is deprecated. In a future version, it will default to False. In addition, specifying 'numeric_only=None' is deprecated. Select only valid columns or specify the value of numeric_only to silence this warning.
  print(df.std())

6.6.2. 函数和说明

下面来了解Python Pandas中描述性统计信息的函数,下表列出了重要函数 -

编号

函数

描述

1

count()

非空观测数量

2

sum()

所有值之和

3

mean()

所有值的平均值

4

median()

所有值的中位数

5

mode()

值的模值

6

std()

值的标准偏差

7

min()

所有值中的最小值

8

max()

所有值中的最大值

9

abs()

绝对值

10

prod()

数组元素的乘积

11

cumsum()

累计总和

12

cumprod()

累计乘积

注 - 由于DataFrame是异构数据结构。通用操作不适用于所有函数。

类似于: sum()cumsum() 函数能与数字和字符(或)字符串数据元素一起工作,不会产生任何错误。 字符聚合从来都比较少被使用,虽然这些函数不会引发任何异常。

由于这样的操作无法执行,因此,当DataFrame包含字符或字符串数据时,像abs(),cumprod()这样的函数会抛出异常。

6.6.3. 汇总数据

describe() 函数是用来计算有关DataFrame列的统计信息的摘要。

>>> df.describe()
Age Rating
count 12.000000 12.000000
mean 31.833333 3.743333
std 9.232682 0.661628
min 23.000000 2.560000
25% 25.000000 3.230000
50% 29.500000 3.790000
75% 35.500000 4.132500
max 51.000000 4.800000

该函数给出了平均值,标准差和IQR值。 而且,函数排除字符列,并给出关于数字列的摘要。 include是用于传递关于什么列需要考虑用于总结的必要信息的参数。获取值列表; 默认情况下是”数字值”。

object - 汇总字符串列
number - 汇总数字列
all - 将所有列汇总在一起(不应将其作为列表值传递)

现在,在程序中使用以下语句并检查输出 -

>>> df.describe(include=['object'])
Name
count 12
unique 12
top Tom
freq 1

现在,使用以下语句并查看输出 -

>>> df. describe(include='all')
Name Age Rating
count 12 12.000000 12.000000
unique 12 NaN NaN
top Tom NaN NaN
freq 1 NaN NaN
mean NaN 31.833333 3.743333
std NaN 9.232682 0.661628
min NaN 23.000000 2.560000
25% NaN 25.000000 3.230000
50% NaN 29.500000 3.790000
75% NaN 35.500000 4.132500
max NaN 51.000000 4.800000