【问题标题】:pandas- calculate percentile (quantile) of grouped columnspandas-计算分组列的百分位数(分位数)
【发布时间】:2020-10-07 16:52:54
【问题描述】:

我的数据框看起来像

lang score
en    0.7
fr    0.4
en    0.3
...
it    0.7
fr    0.2
de    0.5
...

我想获得由score col 分组的lang col 的百分位数(Pandas quantile),因此我计算平均值、中位数和百分位数如下:

mean = df.groupby('lang')['score'].mean().sort_values(ascending=False)
median = df.groupby('lang')['score'].median().sort_values(ascending=False)
perc = df.groupby('lang')['score'].quantile(np.linspace(.1, 1, 9, 0))

虽然mean 和median 是正确的,但我得到一个NaN 用于分位数列:

fr                       0.1                    NaN
                         0.2                    NaN
                         0.3                    NaN
                         0.4                    NaN
                         0.5                    NaN
...                                             ...
en                       0.5                    NaN
                         0.6                    NaN
                         0.7                    NaN
                         0.8                    NaN
                         0.9                    NaN

哪里出错了?

【问题讨论】:

  • 它对我有用。也许你需要更新你的 Pandas。
  • @MykolaZotko 谢谢。我正在使用最新的numpy==1.18.5 和pandas 最新版本。

标签: python pandas quantile percentile


【解决方案1】:

你的数据框中可能有 NaN 吗?

尝试在 perc 计算之前执行此操作:

df.dropna(subset=['score'])

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 2013-06-20
    • 1970-01-01
    • 2017-01-07
    • 2021-11-29
    • 2017-05-17
    相关资源
    最近更新 更多