pandas.DataFrame.quantile#

DataFrame.quantile(q=0.5, axis=0, numeric_only=False, interpolation='linear', method='single')[源代码]#

在指定轴上指定分位数的返回值。

Parameters:
qfloat 或 array-like, 默认 0.5 (50% 分位数)

介于 0 <= q <= 1 之间的值,要计算的分位数。

axis{0 或 ‘index’, 1 或 ‘columns’}, default 0

等于 0 或 ‘index’ 表示逐行计算,1 或 ‘columns’ 表示逐列计算。

numeric_onlybool,默认 False

仅包含 float, intboolean 数据。

在 2.0.0 版本发生变更: numeric_only 的默认值现在是 False

interpolation{‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’}

这个可选参数指定了当所需分位数位于两个数据点 ij 之间时要使用的插值方法:

  • linear: i + (j - i) * fraction,其中 fraction 是位于 ij 之间的索引的分数部分。

  • lower: i

  • higher: j

  • nearest: 最接近的 ij

  • midpoint: (i + j) / 2。

<strong>method</strong>{‘single’, ‘table’}, 默认 ‘single’

是按列 (‘single’) 计算分位数还是跨所有列 (‘table’) 计算分位数。当为 ‘table’ 时,唯一允许的插值方法是 ‘nearest’, ‘lower’, 和 ‘higher’。

Returns:
Series 或 DataFrame
如果 q 是一个数组,将返回一个 DataFrame,其中

索引是 q,列是 self 的列,值是分位数。

如果 q 是一个 float,将返回一个 Series,其中

索引是 self 的列,值是分位数。

参见

core.window.rolling.Rolling.quantile

滚动分位数。

numpy.percentile

计算百分位数的 NumPy 函数。

Examples

>>> df = pd.DataFrame(np.array([[1, 1], [2, 10], [3, 100], [4, 100]]),
...                   columns=['a', 'b'])
>>> df.quantile(.1)
a    1.3
b    3.7
Name: 0.1, dtype: float64
>>> df.quantile([.1, .5])
       a     b
0.1  1.3   3.7
0.5  2.5  55.0

指定 method=’table’ 将跨所有列计算分位数。

>>> df.quantile(.1, method="table", interpolation="nearest")
a    1
b    1
Name: 0.1, dtype: int64
>>> df.quantile([.1, .5], method="table", interpolation="nearest")
     a    b
0.1  1    1
0.5  3  100

指定 numeric_only=False 也会计算 datetime 和 timedelta 数据的分位数。

>>> df = pd.DataFrame({'A': [1, 2],
...                    'B': [pd.Timestamp('2010'),
...                          pd.Timestamp('2011')],
...                    'C': [pd.Timedelta('1 days'),
...                          pd.Timedelta('2 days')]})
>>> df.quantile(0.5, numeric_only=False)
A                    1.5
B    2010-07-02 12:00:00
C        1 days 12:00:00
Name: 0.5, dtype: object