开始使用免费开始使用

在分布中突出数值

有时需要先整理数据,才能做出更好的可视化。处理缺失值的两个方法是 .dropna().fillna()。您还可以通过在特定列上使用 .quantile() 设定条件,过滤高于或低于某个百分位的记录,从而去除离群值。

您还在视频中看到如何在图中强调某个特定取值:在位置 x 处跨坐标轴添加一条竖直线:

Axes.axvline(x=0, color=None, ...)

本练习中,您将最后看一眼全球收入分布,然后删除高于第 95 个百分位的离群值,绘制分布图,并同时突出显示均值和中位数。已导入 pandaspdseabornsnsmatplotlib.pyplotplt,之前练习中的 income DataFrame 已在您的工作区可用。

本练习是课程的一部分

在 Python 中导入与管理金融数据

查看课程

练习说明

  • 将列 'Income per Capita' 赋给变量 inc_per_capita
  • 过滤并仅保留 inc_per_capita 中低于第 95 个百分位的行,并将结果重新赋值给同名变量。
  • 为过滤后的 inc_per_capita 绘制默认直方图,并将轴对象赋给 ax
  • 使用 ax.axvline()color='b',以蓝色突出显示 inc_per_capita 的均值。
  • 使用 ax.axvline()color='g',以绿色突出显示其中位数。显示结果!

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create inc_per_capita
inc_per_capita = ____

# Filter out incomes above the 95th percentile
inc_per_capita = inc_per_capita[____ < ____]

# Plot histogram and assign to ax
ax = ____

# Highlight mean
ax.axvline(inc_per_capita.mean(), color='b')

# Highlight median
ax.axvline(inc_per_capita.median(), color='g')

# Show the plot
plt.show()
编辑并运行代码