有不少方法用来集体计算DataFrame
的描述性统计信息和其余相关操做。 其中大多数是sum()
,mean()
等聚合函数。 通常来讲,这些方法采用轴参数,就像ndarray.{sum,std,...}
,但轴能够经过名称或整数来指定:shell
下面建立一个数据帧(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
返回所请求轴的值的总和。 默认状况下,轴为列名(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())
输出结果:spa
Age 382 Name TomJamesRickyVinSteveMinsuJackLeeDavidGasperBe... Rating 44.92 dtype: object
示例axis=1code
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()
返回平均值blog
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
返回标准差。pandas
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()
函数能与数字和字符(或)字符串数据元素一块儿工做,不会产生任何错误。字符聚合历来都比较少被使用,虽然这些函数不会引起任何异常。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