DataFrame.quantile(self, q=0.5, axis=0, numeric_only=True, interpolation='linear') [source]
返回在请求轴上的给定的quantile值。
参数: | q : float 或 array-like, 默认 要计算的quantile值在 axis : 行为 numeric_only : 如果为 interpolation : { 这个可选参数指定了当所需quantile位于两个数据点i和j之间时要使用的插值方法: 1) linear: 2) lower: 3) higher: 4) nearest: 5) midpoint: |
返回值: |
如果
如果
|
例子
import pandas as pd # 示例数据 df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': [10, 20, 30, 40], 'C': [100, 50, 30, 20] }) # 计算每列的中位数(默认 q=0.5) print(df.quantile()) # 计算 25%、50%、75% 分位数 print(df.quantile([0.25, 0.5, 0.75])) # 按行计算中位数 print(df.quantile(q=0.5, axis=1))
包括日期时间与时间增量列,设置 numeric_only=False
import pandas as pd df2 = pd.DataFrame({ 'A': [1, 2], 'B': [pd.Timestamp('2010'), pd.Timestamp('2011')], 'C': [pd.Timedelta('1 days'), pd.Timedelta('2 days')] }) # 计算中位数(q=0.5),包含所有可支持类型 print(df2.quantile(0.5, numeric_only=False))