python - 如何获取箱线图中每个中位数的值?

标签 python pandas boxplot

数据集来自kaggle .

这段代码

melbourne_file_path = './melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path) 
filtered_melbourne_data = melbourne_data.dropna(axis=0)
ax = filtered_melbourne_data.boxplot(column = 'Price', by = 'Regionname');

给出这个箱线图

enter image description here

箱线图已经有很多信息,例如中位数,有没有办法让它们与by相对应?

我尝试了改编自此的代码 post

ax, bp = filtered_melbourne_data.boxplot(column = 'Price', by = 'Regionname', return_type='both');

并收到此错误

ValueError: not enough values to unpack (expected 2, got 1)

我还尝试了改编自该帖子的代码。

ax = filtered_melbourne_data.boxplot(column = 'Price', by = 'Regionname', return_type='both');
print(ax.median)

得到了

<bound method Series.median of Price    (AxesSubplot(0.1,0.15;0.8x0.75), {'whiskers': ...
dtype: object>

如何获取每个Regionname的中位数值?

最佳答案

这是可能的,但需要对帖子中的解决方案进行一些更改:

首先添加['Price']以从一个元素获取值Series:

ax, bp = filtered_melbourne_data.boxplot(column = 'Price', 
                                         by = 'Regionname', 
                                         return_type='both')['Price']

然后通过索引获取数组的第一个值 - [0]:

medians = [median.get_ydata()[0] for median in bp["medians"]]
print (medians)
[990000.0, 670000.0, 715000.0, 590000.0, 780000.0, 1230000.0, 700000.0, 400000.0]

关于python - 如何获取箱线图中每个中位数的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58263530/

相关文章:

r - R中的箱线图显示平均值

python - Seaborn 和 Pandas,分组箱线图

python - 如何使用python从文本文件制作字典

python - 如何提取字符串中整数值的第二个实例

python - 在 pandas DataFrame 中提取最佳性能代理的时间序列

python - .json 扩展文件 + 时间戳 + Pandas + Python

python - pandas/python 中的箱线图如何工作?

python - python Abaqus 内存不足

python - 在 Django 1.10 中将三元组与排名搜索相结合

python - 如何使用重定义一个方法,在方法中仍然使用原来未修改的继承方法?