获取箱线图的数据 - Pandas

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33518472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:10:05  来源:igfitidea点击:

Getting Data of a boxplot - Pandas

pythonpandas

提问by Manura Omal

I need to get the statistical data which were generated to draw a box plot in Pandas(using dataframe to create boxplots). i.e. Quartile1,Quartile2,Quartile3, lower whisker value, upper whisker value and outliers. I tried the following query to draw the boxplot.

我需要获取生成的统计数据以在 Pandas 中绘制箱线图(使用数据框创建箱线图)。即 Quartile1、Quartile2、Quartile3、下须值、上须值和异常值。我尝试了以下查询来绘制箱线图。

import pandas as pd
df = pd.DataFrame(np.random.rand(100, 5), columns=['A', 'B', 'C', 'D', 'E'])
pd.DataFrame.boxplot(df,return_type = 'both')

Is there a way to do it instead of manually calculating the values?

有没有办法代替手动计算值?

回答by philngo

One option is to use the y data from the plots - probably most useful for the outliers (fliers)

一种选择是使用图中的 y 数据 - 可能对异常值(传单)最有用

_, bp = pd.DataFrame.boxplot(df, return_type='both')

outliers = [flier.get_ydata() for flier in bp["fliers"]]
boxes = [box.get_ydata() for box in bp["boxes"]]
medians = [median.get_ydata() for median in bp["medians"]]
whiskers = [whiskers.get_ydata() for whiskers in bp["whiskers"]]

But it's probably more straightforward to get the other values (including IQR) using either

但是使用以下任一方法获取其他值(包括 IQR)可能更直接

quantiles = df.quantile([0.01, 0.25, 0.5, 0.75, 0.99])

or, as suggested by WoodChopper

或者,按照 WoodChopper 的建议

stats = df.describe()