开始使用免费开始使用

全球收入分布的十分位数

十分位数(decile) 是一种特殊的分位数,通过将某个数据集的分布划分为 10 份得到。可以通过向 .quantile() 传入下面的 numpy 函数来创建十分位数(以及其他任意分位数),其中 start 是区间起点(包含),stop 是区间终点(不包含),step 是相邻数值之间的间隔:

np.arange(start, stop, step)

正如您在视频中看到的,标准柱状图非常适合可视化数据分布。您可以在 .plot() 中添加参数 kind='bar' 来创建柱状图。

现在请您动手,绘制以十分位数概括的收入分布吧!pandas 已以 pd 导入,numpy 已以 np 导入,matplotlib.pyplot 已以 plt 导入,且上一个练习中的 income DataFrame 已在您的工作空间中可用。

本练习是课程的一部分

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

查看课程

练习说明

  • 使用 np.arange() 生成从 10% 到 90%、步长为 10% 的百分位,赋值给 quantiles,并打印。
  • 使用 quantiles.quantile() 计算人均收入的十分位数,保存为 deciles,并打印结果。
  • 将结果绘制为柱状图并显示,使用 plt.tight_layout()。将标题设为 'Global Income per Capita - Deciles'

交互式实操练习

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

# Generate range of deciles
quantiles = ____

# Print them
print(quantiles)

# Calculate deciles for 'Income per Capita'
deciles = ____.quantile(____)

# Print them
print(deciles)

# Plot deciles as a bar chart
deciles.____(____=____, title='Global Income per Capita - Deciles')

# Make sure to use the tight layout!
plt.____()

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