59_Pandas中使用describe获取每列的汇总统计信息(平均值、标准差等)
使用 pandas.DataFrame 和 pandas.Series 的 describe() 方法,您可以获得汇总统计信息,例如每列的均值、标准差、最大值、最小值和众数。
在此,对以下内容进行说明。
- describe() 的基本用法
- 指定目标类型:include、exclude
- 指定非数字列,例如字符串
- 指定所有类型的列
- 选择/排除任何类型
- describe() 项目的含义和相应的个别方法
- count:元素的数量
- unique:具有唯一(unique)值的元素的数量
- top:mode
- freq:mode(出现次数)
- mean:算术平均值
- std:标准偏差
- min:最小值
- max:最大值
- 50%:中位数
- 25%、75%:1/4 分位数、3/4 分位数
- 指定百分位增量
- 计算数字列的频率等
- 计算一串数字的平均值、标准差等
- pandas.Series 中的 describe()
- 日期和时间(datetime64[ns] 类型)
- 将 describe() 应用于行
示例代码中,以每列具有不同类型 dtype 的 pandas.DataFrame 为例。
import pandas as pd
df = pd.DataFrame({
'a': [1, 2, 1, 3],
'b': [0.4, 1.1, 0.1, 0.8],
'c': ['X', 'Y', 'X', 'Z'],
'd': ['3', '5', '2', '1'],
'e': [True, True, False, True]})
print(df)
# a b c d e
# 0 1 0.4 X 3 True
# 1 2 1.1 Y 5 True
# 2 1 0.1 X 2 False
# 3 3 0.8 Z 1 True
print(df.dtypes)
# a int64
# b float64
# c object
# d object
# e bool
# dtype: object
describe() 的基本用法
指定目标类型:include、exclude
指定非数字列,例如字符串
指定所有类型的列
选择/排除任何类型
describe() 项目的含义和相应的个别方法
count:元素的数量
unique:具有唯一(unique)值的元素的数量
top:mode
freq:mode(出现次数)
mean:算术平均值
std:标准偏差
min:最小值
max:最大值
50%:中位数
25%、75%:1/4 分位数、3/4 分位数
指定百分位增量
计算数字列的频率等
计算一串数字的平均值、标准差等
pandas.Series 中的 describe()
日期和时间(datetime64[ns] 类型)
将 describe() 应用于行
describe() 的基本用法
在示例 pandas.DataFrame 中调用不带任何参数的 describe() 方法会将结果作为 pandas.DataFrame 返回。
print(df.describe())
# a b
# count 4.000000 4.000000
# mean 1.750000 0.600000
# std 0.957427 0.439697
# min 1.000000 0.100000
# 25% 1.000000 0.325000
# 50% 1.500000 0.600000
# 75% 2.250000 0.875000
# max 3.000000 1.100000
print(type(df.describe()))
# <class 'pandas.core.frame.DataFrame'>
可以使用 loc 和 at 获取行和元素。
print(df.describe().loc['std'])
# a 0.957427
# b 0.439697
# Name: std, dtype: float64
print(df.describe().at['std', 'b'])
# 0.439696865275764
对于pandas.DataFrame,有各种类型的列,默认只选择数值列(整数类型int,浮点类型float),计算均值和标准差std等项。项目的含义将在后面解释。
由于严格按照类型dtype进行判断,所以排除了像例子中的d列这样的数字字符串的列。
任何缺失值 NaN 都被排除在计算之外。
指定目标类型:include、exclude
要获取非数字列的汇总统计信息,请设置参数 include 和 exclude。
include 指定要包含在结果中的类型,exclude 指定要从结果中排除的类型。请注意,指定的是类型,而不是列名。
指定非数字列,例如字符串
数字类型由“数字”表示,因此使用 exclude=‘number’ 将计算非数字列(例如字符串类型)的结果。
print(df.describe(exclude='number'))
# c d e
# count 4 4 4
# unique 3 4 2
# top X 3 True
# freq 2 1 3
从结果可以看出,数值和其他计算的项目是不同的。项目的含义将在后面解释。
另外,pandas.DataFrame本来就不包含数字列的情况下,不设置任何参数也是可以的。
df_notnum = df[['c', 'd', 'e']]
print(df_notnum)
# c d e
# 0 X 3 True
# 1 Y 5 True
# 2 X 2 False
# 3 Z 1 True
print(df_notnum.dtypes)
# c object
# d object
# e bool
# dtype: object
print(df_notnum.describe())
# c d e
# count 4 4 4
# unique 3 4 2
# top X 3 True
# freq 2 1 3
指定所有类型的列
如果 include=‘all’,则包括所有类型的列。
print(df.describe(include='all'))
# a b c d e
# count 4.000000 4.000000 4 4 4
# unique NaN NaN 3 4 2
# top NaN NaN X 3 True
# freq NaN NaN 2 1 3
# mean 1.750000 0.600000 NaN NaN NaN
# std 0.957427 0.439697 NaN NaN NaN
# min 1.000000 0.100000 NaN NaN NaN
# 25% 1.000000 0.325000 NaN NaN NaN
# 50% 1.500000 0.600000 NaN NaN NaN
# 75% 2.250000 0.875000 NaN NaN NaN
# max 3.000000 1.100000 NaN NaN NaN
但是,由于数值型列和其他类型的列计算的项不同,所以没有计算的项的值会是缺失值NaN。
选择/排除任何类型
可以为参数 include 和 exclude 指定任意类型。即使结果只有一列,也会返回 pandas.DataFrame 类型。
print(df.describe(include=int))
# a
# count 4.000000
# mean 1.750000
# std 0.957427
# min 1.000000
# 25% 1.000000
# 50% 1.500000
# 75% 2.250000
# max 3.000000
print(type(df.describe(include=int)))
# <class 'pandas.core.frame.DataFrame'>
可以在列表中指定多个类型。要计算的项目由所选类型自动确定。
print(df.describe(include=[object, bool]))
# c d e
# count 4 4 4
# unique 3 4 2
# top X 3 True
# freq 2 1 3
print(df.describe(exclude=[float, object]))
# a e
# count 4.000000 4
# unique NaN 2
# top NaN True
# freq NaN 3
# mean 1.750000 NaN
# std 0.957427 NaN
# min 1.000000 NaN
# 25% 1.000000 NaN
# 50% 1.500000 NaN
# 75% 2.250000 NaN
# max 3.000000 NaN
‘number’ 和 ‘all’ 必须用引号 ’ 或 " 括起来,但是 Python 标准内置类型如 int 和 float 可以指定为 include=int 不带引号。 include=‘int’ 但没问题。
describe() 项目的含义和相应的个别方法
描述 describe() 计算的项目的含义以及当您只想单独计算每个项目时可以使用的方法。
如果用loc[]指定describe()获取的pandas.DataFrame行,可以选择每一项的值,但如果不需要其他项,单独使用方法比较浪费。
上面说了,注意describe()计算出来的item对于数值和其他类型是不一样的。此外,在 describe() 中,计算任何项目时都会排除缺失值 NaN。
count:元素的数量
可以使用 count() 方法单独计算。
print(df.count())
# a 4
# b 4
# c 4
# d 4
# e 4
# dtype: int64
unique:具有唯一(unique)值的元素的数量
可以使用 nunique() 方法单独计算。
mode() 返回一个 pandas.DataFrame。如果有相同频率的值,则有多种模式,每列的模式数不同。
print(df.nunique())
# a 3
# b 4
# c 3
# d 4
# e 2
# dtype: int64
top:mode
可以使用 mode() 方法单独计算。
print(df.mode())
# a b c d e
# 0 1.0 0.1 X 1 True
# 1 NaN 0.4 NaN 2 NaN
# 2 NaN 0.8 NaN 3 NaN
# 3 NaN 1.1 NaN 5 NaN
可以通过对 mode() 的结果应用 count() 方法来获得每列中模态值的数量,该方法计算不是缺失值 NaN 的元素的数量。
print(df.mode().count())
# a 1
# b 4
# c 1
# d 4
# e 1
# dtype: int64
如果使用 iloc[0] 选择第一行,则每一列至少可以获得一个模式值。
print(df.mode().iloc[0])
# a 1
# b 0.1
# c X
# d 1
# e True
# Name: 0, dtype: object
如果describe()的item top有多个mode值,则只返回其中一个,但要注意,它并不总是匹配mode()的第一行。
freq:mode(出现次数)
可以使用 pandas.Series 的 value_counts() 方法单独计算。
value_counts() 返回一个 pandas.Series,其唯一元素值为 index,其频率(出现次数)为 data。
默认情况下,pandas.Series 按出现的降序排序,因此 value_counts() 方法返回的 pandas.Series 中的第一个值是模式的频率。
print(df['c'].value_counts().iat[0])
# 2
如果要获取 pandas.DataFrame 的每一列的模式频率,请使用 apply() 方法应用一个匿名函数,该函数将 value_counts() 方法的结果中的最高值返回到每一列。
print(df.apply(lambda x: x.value_counts().iat[0]))
# a 2
# b 1
# c 2
# d 1
# e 3
# dtype: int64
在 value_counts() 中,原始 pandas.Series 的元素是生成的 pandas.Series 的索引。如果数值为index,则不能用[number]指定值(会报错),所以使用iat [number]严格指定。
mean:算术平均值
可以使用 mean() 方法单独计算。
参数 numeric_only=True 仅产生数字列。以下方法相同。
print(df.mean(numeric_only=True))
# a 1.75
# b 0.60
# e 0.75
# dtype: float64
bool 类型列在 describe() 中被排除,但在 mean() 中被视为 True=1,False=0。以下方法相同。
std:标准偏差
除以n-1,也叫样本标准差。
可以使用 std() 方法单独计算。
print(df.std(numeric_only=True))
# a 0.957427
# b 0.439697
# e 0.500000
# dtype: float64
min:最小值
它可以使用 min() 方法单独计算。
print(df.min(numeric_only=True))
# a 1.0
# b 0.1
# e 0.0
# dtype: float64
max:最大值
可以使用 max() 方法单独计算。
print(df.max(numeric_only=True))
# a 3.0
# b 1.1
# e 1.0
# dtype: float64
50%:中位数
也称为 1/2 分位数或第 50 个百分位数。
它可以用 median() 方法单独计算。
print(df.median(numeric_only=True))
# a 1.5
# b 0.6
# e 1.0
# dtype: float64
25%、75%:1/4 分位数、3/4 分位数
也称为第 1 个四分位数、第 3 个四分位数、第 25 个百分位数、第 75 个百分位数等。
可以使用 quantile() 方法单独计算。指定要计算的百分位数列表,范围从0到1。
print(df.quantile(q=[0.25, 0.75], numeric_only=True))
# a b e
# 0.25 1.00 0.325 0.75
# 0.75 2.25 0.875 1.00
还可以使用 quantile() 一次性计算最小值、最大值和中值。
print(df.quantile(q=[0, 0.25, 0.5, 0.75, 1], numeric_only=True))
# a b e
# 0.00 1.00 0.100 0.00
# 0.25 1.00 0.325 0.75
# 0.50 1.50 0.600 1.00
# 0.75 2.25 0.875 1.00
# 1.00 3.00 1.100 1.00
也可以在 describe() 中使用以下参数 percentiles 指定要计算的百分位数。
指定百分位增量
与前面的示例一样,describe() 默认计算最小值(第 0 个百分位)、中值(第 50 个百分位)、最大值(第 100 个百分位)以及第 25 个和第 75 个百分位。
始终计算最小值、中值和最大值,但可以使用参数百分位数指定其他值。指定范围从 0 到 1 的值列表。
print(df.describe(percentiles=[0.2, 0.4, 0.6, 0.8]))
# a b
# count 4.000000 4.000000
# mean 1.750000 0.600000
# std 0.957427 0.439697
# min 1.000000 0.100000
# 20% 1.000000 0.280000
# 40% 1.200000 0.480000
# 50% 1.500000 0.600000
# 60% 1.800000 0.720000
# 80% 2.400000 0.920000
# max 3.000000 1.100000
计算数字列的频率等
例如,对于男性用 0 表示,女性用 1 表示的分类数据,或者地名分配给数值的分类数据,有些情况下我们想要检查众数及其频率而不是均值和标准差,即使对于数值数据。
print(df.astype('str').describe())
# a b c d e
# count 4 4 4 4 4
# unique 3 4 3 4 2
# top 1 1.1 X 3 True
# freq 2 1 2 1 3
print(df.astype({
'a': str}).describe(exclude='number'))
# a c d e
# count 4 4 4 4
# unique 3 3 4 2
# top 1 X 3 True
# freq 2 2 1 3
计算一串数字的平均值、标准差等
同样,如果要计算一串数字的平均值或标准偏差,请使用 astype() 方法。
print(df.astype({
'd': int, 'e': int}).describe())
# a b d e
# count 4.000000 4.000000 4.000000 4.00
# mean 1.750000 0.600000 2.750000 0.75
# std 0.957427 0.439697 1.707825 0.50
# min 1.000000 0.100000 1.000000 0.00
# 25% 1.000000 0.325000 1.750000 0.75
# 50% 1.500000 0.600000 2.500000 1.00
# 75% 2.250000 0.875000 3.500000 1.00
# max 3.000000 1.100000 5.000000 1.00
pandas.Series 中的 describe()
pandas.Series 也有一个 describe() 方法。返回 pandas.Series。
s_int = df['a']
print(s_int)
# 0 1
# 1 2
# 2 1
# 3 3
# Name: a, dtype: int64
print(s_int.describe())
# count 4.000000
# mean 1.750000
# std 0.957427
# min 1.000000
# 25% 1.000000
# 50% 1.500000
# 75% 2.250000
# max 3.000000
# Name: a, dtype: float64
print(type(s_int.describe()))
# <class 'pandas.core.series.Series'>
参数 include 和 exclude 被忽略,项目根据类型 dtype 计算。也可以使用 astype() 进行类型转换。
s_str = df['d']
print(s_str.describe())
# count 4
# unique 4
# top 3
# freq 1
# Name: d, dtype: object
print(s_str.astype('int').describe())
# count 4.000000
# mean 2.750000
# std 1.707825
# min 1.000000
# 25% 1.750000
# 50% 2.500000
# 75% 3.500000
# max 5.000000
# Name: d, dtype: float64
日期和时间(datetime64[ns] 类型)
为 datetime64[ns] 类型的列添加了第一项和最后一项。
df['dt'] = pd.to_datetime(['2018-01-01', '2018-03-15', '2018-02-20', '2018-03-15'])
print(df.dtypes)
# a int64
# b float64
# c object
# d object
# e bool
# dt datetime64[ns]
# dtype: object
print(df.describe(include='datetime'))
# dt
# count 4
# unique 3
# top 2018-03-15 00:00:00
# freq 2
# first 2018-01-01 00:00:00
# last 2018-03-15 00:00:00
从字面上看,first 表示第一个日期,last 表示最后一个日期。可以用 min() 和 max() 分别计算。
print(df['dt'].min())
# 2018-01-01 00:00:00
print(df['dt'].max())
# 2018-03-15 00:00:00
将 describe() 应用于行
describe() 没有指定行和列的参数轴。 使用 .T 进行转置,然后调用 describe() 以获得原始行的结果。
print(df.T.describe())
# 0 1 2 3
# count 6 6 6 6
# unique 5 6 6 6
# top 1 2018-03-15 00:00:00 2018-02-20 00:00:00 2018-03-15 00:00:00
# freq 2 1 1 1
在 pandas 中,每一列都有一个类型 dtype,所以基本上假设每一列都排列了相同类型的数据。
因此,通常没有必要对每一行都获取汇总统计信息,如果每一行都排列着同一种数据,最好进行转置,这样更容易进行各种处理,而不仅仅是 describe()。