有很多方法用来集体计算DataFrame
的描述性统计信息和其他相关操作。 其中大多数是sum()
,mean()
等聚合函数。 一般来说,这些方法采用轴参数,就像ndarray.{sum,std,...}
,但轴可以通过名称或整数来指定:
- 数据帧(DataFrame) - “index”(axis=0,默认),columns(axis=1)
下面创建一个数据帧(DataFrame),并使用此对象进行演示本章中所有操作。
import pandas as pd 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])} df = pd.DataFrame(d) print(df)
输出结果:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
7 34 Lee 3.78
8 40 David 2.98
9 30 Gasper 4.80
10 51 Betina 4.10
11 46 Andres 3.65
sum()
返回所请求轴的值的总和。 默认情况下,轴为列名(axis=0
)。
import pandas as pd 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])} df = pd.DataFrame(d) print(df.sum())
输出结果:
Age 382
Name TomJamesRickyVinSteveMinsuJackLeeDavidGasperBe...
Rating 44.92
dtype: object
示例axis=1
import pandas as pd 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])} df = pd.DataFrame(d) print(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()
返回平均值
import pandas as pd 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])} df = pd.DataFrame(d) print(df.mean())
输出结果:
Age 31.833333
Rating 3.743333
dtype: float64
std()
返回标准差。
import pandas as pd 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])} df = pd.DataFrame(d) print(df.std())
输出结果:
Age 9.232682
Rating 0.661628
dtype: float64
函数和说明
下面来了解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()
这样的函数会抛出异常。
汇总数据
describe()
函数是用来计算有关DataFrame列的统计信息的摘要。
1. 描述数字系列
import pandas as pd 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])} df = pd.DataFrame(d) print(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
2. 描述一个分类系列
import pandas as pd s = pd.Series(['a', 'a', 'b', 'c']) print(s.describe())
输出结果:
count 4
unique 3
top a
freq 2
dtype: object
其结果包括count,unique,top,和freq。时间数据还包括first和last项目。
count:同上
unique:表示有多少种不同的值
top:数据中出现次数最高的值
freq:出现次数最高的那个值(top)的出现频率
3. 描述时间戳系列
import pandas as pd import numpy as np s = pd.Series([np.datetime64("2000-01-01"), np.datetime64("2010-01-01"), np.datetime64("2010-01-01") ]) print(s.describe())
输出结果:
count 3
unique 2
top 2010-01-01 00:00:00
freq 2
first 2000-01-01 00:00:00
last 2010-01-01 00:00:00
dtype: object
使用include和exclude参数来限制DataFrame中哪些列被分析输出
object
- 汇总字符串列number
- 汇总数字列all
- 将所有列汇总在一起(不应将其作为列表值传递)
(1)如果include ='all'作为选项提供,所有列,而不管数据类型如何。
import pandas as pd 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])} df = pd.DataFrame(d) print(df.describe(include='all'))
输出结果:
Name Age Rating
count 12 12.000000 12.000000
unique 12 NaN NaN
top Steve 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
(2)在DataFrame描述中只包含字符串列
import pandas as pd import numpy as np 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])} df = pd.DataFrame(d) print(df.describe(include=[np.object]))
(3)在DataFrame描述中仅包含数字列
import pandas as pd import numpy as np 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])} df = pd.DataFrame(d) print(df.describe(include=[np.number]))
输出结果:
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
从DataFrame描述中排除对象列。
import pandas as pd import numpy as np 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])} df = pd.DataFrame(d) print(df.describe(exclude=[np.object]))
输出结果:
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
import pandas as pd 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])} df = pd.DataFrame(d) print(df.describe(include=['object']))
输出结果:
Name
count 12
unique 12
top Ricky
freq 1