全球所得分配的十分位數
「十分位數(decile)」是一種特殊的分位數,透過將某個資料集的分配切成十等分而得。你可以將下列 numpy 函式提供給 .quantile() 來建立十分位數(以及其他任何分位數),其中 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()