Skip to main content
 首页 » 编程设计

python之过滤掉 np.percentile 中的零

2025年05月04日24wuhuacong

我正在尝试对 DataFrame 的列 score 进行等分。

我使用以下代码:

np.percentile(df['score'], np.arange(0, 100, 10)) 

我的问题是分数,有很多零。如何过滤掉这些 0 值并只对其余值进行十分位数处理?

请您参考如下方法:

使用 bool 索引过滤它们:

df.loc[df['score']!=0, 'score'] 

或

df['score'][lambda x: x!=0] 

并将其传递给百分位数函数。

np.percentile(df['score'][lambda x: x!=0], np.arange(0,100,10))