大 pandas :查找给定列的百分位数统计 [英] pandas: find percentile stats of a given column

查看:52
本文介绍了大 pandas :查找给定列的百分位数统计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框my_df,在这里我可以找到给定列的mean(),median(),mode():

I have a pandas data frame my_df, where I can find the mean(), median(), mode() of a given column:

my_df['field_A'].mean()
my_df['field_A'].median()
my_df['field_A'].mode()

我想知道是否可以找到更详细的统计数据,例如90%?谢谢!

I am wondering is it possible to find more detailed stats such as 90 percentile? Thanks!

推荐答案

您可以使用

You can use the pandas.DataFrame.quantile() function, as shown below.

import pandas as pd
import random

A = [ random.randint(0,100) for i in range(10) ]
B = [ random.randint(0,100) for i in range(10) ]

df = pd.DataFrame({ 'field_A': A, 'field_B': B })
df
#    field_A  field_B
# 0       90       72
# 1       63       84
# 2       11       74
# 3       61       66
# 4       78       80
# 5       67       75
# 6       89       47
# 7       12       22
# 8       43        5
# 9       30       64

df.field_A.mean()   # Same as df['field_A'].mean()
# 54.399999999999999

df.field_A.median() 
# 62.0

# You can call `quantile(i)` to get the i'th quantile,
# where `i` should be a fractional number.

df.field_A.quantile(0.1) # 10th percentile
# 11.9

df.field_A.quantile(0.5) # same as median
# 62.0

df.field_A.quantile(0.9) # 90th percentile
# 89.10000000000001

这篇关于大 pandas :查找给定列的百分位数统计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆