依產業別與 IPO 年計算多項指標
seaborn 的 pointplot() 函式可用來比較數值變數在不同類別變數層級下的彙總統計量:
seaborn.pointplot(x=None, y=None, hue=None, data=None, ...)
在影片中,你看到以是否在 2000 年以前(第一個層級)或之後(第二個層級)上市(IPO,為類別變數)來區分的市值(數值變數)視覺化。
在這個練習中,你要比較自 2000 年以來,每一年 NYSE 與 NASDAQ 的平均市值,並排除高於第 95 百分位數的離群值。已經匯入 pandas 作為 pd 與 matplotlib.pyplot 作為 plt,而且工作環境中已提供包含參考欄位 'Exchange' 的 listings DataFrame。
本練習屬於課程
在 Python 中匯入與管理財務資料
練習說明
- 匯入
seaborn並命名為sns。 - 從所有交易所中篩選出 2000 年之後 IPO 的公司,但排除
'amex'。 - 將
'IPO Year'欄位的資料轉為整數。 - 建立欄位
market_cap_m,以「美元百萬」表示市值。 - 在
market_cap_m中排除高於第 95 百分位數的值。 - 以
listings建立pointplot,x使用'IPO Year',y使用'market_cap_m',hue使用'Exchange'。將xticks旋轉 45 度後顯示結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the seaborn library as sns
____
# Exclude IPOs before 2000 and from the 'amex'
listings = ____[(____['IPO Year'] > ____) & (listings.Exchange != ____)]
# Convert IPO Year to integer
listings['IPO Year'] = ____['IPO Year'].____(____)
# Create market_cap_m
listings['market_cap_m'] = ____['Market Capitalization'].div(1e6)
# Exclude outliers
listings = listings[listings.____ < listings.____.____(.95)]
# Create the pointplot
sns.pointplot(x=____, y=____, hue=____, data=____)
# Rotate xticks
plt.____(____=____)
# Show the plot
plt.show()