使用 keys 進行串接(concatenate)
音樂串流公司的管理層請你協助分析最近一個商務季度的銷售表現。他們想知道該季度中,哪一個月份的發票總額平均值最高。
你拿到三個包含發票資料的資料表,名稱分別為 inv_jul、inv_aug 和 inv_sep。請將這三個資料表串接為一個,並繪製各月份發票總額平均值的圖表。
本練習屬於課程
使用 pandas 進行資料表連接
練習說明
- 將三個資料表依序垂直串接,最早的月份在前,並分別加入
'7Jul'、'8Aug'、'9Sep'作為各自月份的keys,將結果儲存為inv_jul_thr_sep。 - 使用
.agg()方法,從分組後的發票資料中計算total欄位的平均值。 - 以
avg_inv_by_month繪製長條圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Concatenate the tables and add keys
inv_jul_thr_sep = pd.concat(____,
keys=____)
# Group the invoices by the index keys and find avg of the total column
avg_inv_by_month = inv_jul_thr_sep.groupby(level=0).agg({'total':'----'})
# Bar plot of avg_inv_by_month
avg_inv_by_month.____
plt.show()