pandas.DataFrame.quantile#

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

返回所请求轴上的分位数(百分位数)值。

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

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

axis{0 或 ‘index’, 1 或 ‘columns’}, 默认为 0

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

numeric_onlybool, default False

仅包含 floatintboolean 数据。

版本 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。

method{‘single’, ‘table’}, 默认值 ‘single’

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

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

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

如果 q 是一个浮点数,则将返回一个 Series,其中

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

另请参阅

core.window.rolling.Rolling.quantile

滚动分位数。

numpy.percentile

计算百分位数的 Numpy 函数。

示例

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

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

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

指定 numeric_only=False 将为所有列计算分位数。

>>> 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