当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python pandas.core.groupby.DataFrameGroupBy.describe用法及代码示例


用法:

DataFrameGroupBy.describe(**kwargs)

生成说明性统计数据。

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

分析数字和对象系列,以及混合数据类型的DataFrame 列集。输出将根据提供的内容而有所不同。有关详细信息,请参阅下面的注释。

参数

percentileslist-like 个数字,可选

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

include‘all’, list-like of dtypes 或 None(默认),可选

要包含在结果中的数据类型白名单。忽略 Series 。以下是选项:

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

  • A list-like of dtypes:将结果限制为提供的数据类型。要将结果限制为数字类型,请提交 numpy.number 。要将其限制为对象列,请提交 numpy.object 数据类型。字符串也可以以 select_dtypes 的样式使用(例如 df.describe(include=['O']) )。要选择 pandas 分类列,请使用 'category'

  • 无(默认):结果将包括所有数字列。

excludelist-like of dtypes 或 None(默认),可选,

要从结果中省略的数据类型的黑名单。忽略 Series 。以下是选项:

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

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

datetime_is_numeric布尔值,默认为 False

是否将 datetime dtypes 视为数字。这会影响为列计算的统计信息。对于 DataFrame 输入,这还控制默认情况下是否包含日期时间列。

返回

Series或DataFrame

提供的系列或 DataFrame 的汇总统计信息。

注意

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

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

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

对于通过 DataFrame 提供的混合数据类型,默认情况下仅返回对数值列的分析。如果 DataFrame 仅包含对象和分类数据而没有任何数字列,则默认返回对对象和分类列的分析。如果 include='all' 作为选项提供,则结果将包括每种类型的属性的联合。

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

例子

说明一个数字 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(datetime_is_numeric=True)
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

相关用法


注:本文由纯净天空筛选整理自pandas.pydata.org大神的英文原创作品 pandas.core.groupby.DataFrameGroupBy.describe。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。