依 IPO 年份的市值中位數
在前一章的最後一節課中,你已經為科技公司的年度 IPO 數量建立了時間軸。
現在我們來分析,不同 IPO 年份的市值如何變化。你可以合併三大交易所的資料,以取得更完整的觀點。
已經為你匯入 pandas 為 pd 與 matplotlib.pyplot 為 plt,而且你的工作環境中也提供了先前練習使用的 listings DataFrame。它現在多了一個參考欄位 'exchange',用來標示每家上市公司的交易所。
本練習屬於課程
在 Python 中匯入與管理財務資料
練習說明
- 使用
.info()與.head()檢視並顯示listings。 - 透過廣播運算,為
listings建立新欄位market_cap_m,其內容為以百萬美元計價的市值。 - 篩選出所有
'IPO Year'大於 1985 的公司。 - 移除
'IPO Year'欄位中的所有遺漏值,並將其餘值轉換為整數dtype。 - 以
'IPO Year'對listings分組,選取market_cap_m欄位並計算median,再用.sort_index()排序,將結果指派給ipo_by_year。 - 以長條圖繪製並顯示結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Inspect listings
listings.____()
# Show listings head
print(listings.____())
# Create market_cap_m
listings['market_cap_m'] = ____[____].div(1e6)
# Select companies with IPO after 1985
listings = listings[____[____] > ____]
# Drop missing values and convert to integers
listings['IPO Year'] = ____[____].dropna().____(int)
# Calculate the median market cap by IPO Year and sort the index
ipo_by_year = listings.____(____).____.median().____()
# Plot results as a bar chart
ipo_by_year.plot(kind='bar')
# Show the plot
plt.show()