pandas.DataFrame.describe#

DataFrame.describe(percentiles=None, include=None, exclude=None)[源代码]#

生成描述性统计信息。

描述性统计数据包括那些总结数据分布的集中趋势、离散程度和形状的统计数据,不包括 NaN 值。

分析数值型和对象型 Series,以及混合数据类型的 DataFrame 列集。输出将根据输入而变化。有关更多详细信息,请参阅下面的注释。

Parameters:
percentiles列表状数字,可选

包含在输出中的百分位数。所有值都应介于 0 和 1 之间。默认为 [.25, .5, .75],返回第 25、50 和第 75 百分位数。

include‘all’,列表状数据类型或 None(默认),可选

白名单数据类型,用于包含在结果中。对于 Series 忽略。选项如下:

  • ‘all’ : 输入的所有列都将包含在输出中。

  • 列表状数据类型 : 将结果限制为提供的数据类型。要将结果限制为数值类型,请提交 numpy.number。要而是将结果限制为对象列,请提交 numpy.object 数据类型。字符串也可用于 select_dtypes 的样式(例如 df.describe(include=['O']))。要选择 pandas 分类列,请使用 'category'

  • None(默认) : 结果将包含所有数值列。

exclude列表状数据类型或 None(默认),可选,

黑名单数据类型,用于从结果中排除。对于 Series 忽略。选项如下:

  • 列表状数据类型 : 从结果中排除提供的数据类型。要排除数值类型,请提交 numpy.number. 要排除对象列,请提交数据类型 numpy.object。字符串也可用于 select_dtypes 的样式(例如 df.describe(exclude=['O']))。要排除 pandas 分类列,请使用 'category'

  • None(默认) : 结果将不排除任何内容。

Returns:
Series 或 DataFrame

提供的 Series 或 DataFrame 的摘要统计信息。

参见

DataFrame.count

计算非 NA/null 观测值的数量。

DataFrame.max

对象中值的最大值。

DataFrame.min

对象中值的最小值。

DataFrame.mean

值的平均值。

DataFrame.std

观测值的标准差。

DataFrame.select_dtypes

根据 dtype 子集化 DataFrame,包含/排除列。

Notes

对于数值数据,结果的索引将包括 countmeanstdminmax 以及较低、50 和较高的百分位数。默认情况下,较低百分位数为 25,较高百分位数为 7550 百分位数与中位数相同。

对于对象数据(例如字符串或时间戳),结果的索引将包括 countuniquetopfreqtop 是最常见的值。freq 是最常见值的频率。时间戳还包括 firstlast 项。

如果有多个对象值具有最高计数,则 counttop 结果将从具有最高计数的那些中任意选择。

对于通过 DataFrame 提供的混合数据类型,默认仅返回数值列的分析。如果 DataFrame 仅包含对象和分类数据而没有数值列,默认将返回对象和分类列的分析。如果提供了 include='all' 选项,结果将包含每种类型的属性的并集。

includeexclude 参数可用于限制 DataFrame 中哪些列用于输出分析。分析 Series 时忽略这些参数。

Examples

描述数值型的 Series

>>> s = pd.Series([1, 2, 3])
>>> s.describe()
count    3.0
mean     2.0
std      1.0
min      1.0
25%      1.5
50%      2.0
75%      2.5
max      3.0
dtype: float64

描述类别型的 Series

>>> s = pd.Series(['a', 'a', 'b', 'c'])
>>> s.describe()
count     4
unique    3
top       a
freq      2
dtype: object

描述时间戳 Series

>>> s = pd.Series([
...     np.datetime64("2000-01-01"),
...     np.datetime64("2010-01-01"),
...     np.datetime64("2010-01-01")
... ])
>>> s.describe()
count                      3
mean     2006-09-01 08:00:00
min      2000-01-01 00:00:00
25%      2004-12-31 12:00:00
50%      2010-01-01 00:00:00
75%      2010-01-01 00:00:00
max      2010-01-01 00:00:00
dtype: object

描述 DataFrame。默认只返回数值类型的字段。

>>> df = pd.DataFrame({'categorical': pd.Categorical(['d', 'e', 'f']),
...                    'numeric': [1, 2, 3],
...                    'object': ['a', 'b', 'c']
...                    })
>>> df.describe()
       numeric
count      3.0
mean       2.0
std        1.0
min        1.0
25%        1.5
50%        2.0
75%        2.5
max        3.0

描述 DataFrame 的所有列,无论数据类型如何。

>>> df.describe(include='all')  
       categorical  numeric object
count            3      3.0      3
unique           3      NaN      3
top              f      NaN      a
freq             1      NaN      1
mean           NaN      2.0    NaN
std            NaN      1.0    NaN
min            NaN      1.0    NaN
25%            NaN      1.5    NaN
50%            NaN      2.0    NaN
75%            NaN      2.5    NaN
max            NaN      3.0    NaN

通过将列作为属性访问来描述 DataFrame 中的一列。

>>> df.numeric.describe()
count    3.0
mean     2.0
std      1.0
min      1.0
25%      1.5
50%      2.0
75%      2.5
max      3.0
Name: numeric, dtype: float64

DataFrame 的描述中只包含数值类型的列。

>>> df.describe(include=[np.number])
       numeric
count      3.0
mean       2.0
std        1.0
min        1.0
25%        1.5
50%        2.0
75%        2.5
max        3.0

DataFrame 的描述中只包含字符串类型的列。

>>> df.describe(include=[object])  
       object
count       3
unique      3
top         a
freq        1

DataFrame 的描述中只包含类别类型的列。

>>> df.describe(include=['category'])
       categorical
count            3
unique           3
top              d
freq             1

DataFrame 的描述中排除数值类型的列。

>>> df.describe(exclude=[np.number])  
       categorical object
count            3      3
unique           3      3
top              f      a
freq             1      1

DataFrame 的描述中排除对象类型的列。

>>> df.describe(exclude=[object])  
       categorical  numeric
count            3      3.0
unique           3      NaN
top              f      NaN
freq             1      NaN
mean           NaN      2.0
std            NaN      1.0
min            NaN      1.0
25%            NaN      1.5
50%            NaN      2.0
75%            NaN      2.5
max            NaN      3.0