使用 seaborn 依類別看分布

在 Python 中匯入與管理財務資料

Stefan Jansen

Instructor

依類別看分布

  • 上一段:摘要統計
  • 觀測數、各類別的平均值
  • 現在:依類別型變數的層級,比較某變數的分布
  • 範例:各「Sector」或「IPO Year」的市值分布
  • 比摘要統計更有細節
在 Python 中匯入與管理財務資料

清理資料:移除離群值

nasdaq = pd.read_excel('listings.xlsx', sheet_name='nasdaq', 
                        na_values='n/a')
nasdaq['market_cap_m'] = nasdaq['Market Capitalization'].div(1e6)

nasdaq = nasdaq[nasdaq.market_cap_m > 0] # Active companies only
outliers = nasdaq.market_cap_m.quantile(.9) # Outlier threshold
nasdaq = nasdaq[nasdaq.market_cap_m < outliers] # Remove outliers
在 Python 中匯入與管理財務資料

盒鬚圖:四分位與離群值

import seaborn as sns
sns.boxplot(x='Sector', y='market_cap_m', data=nasdaq)
plt.xticks(rotation=75);

四分位與離群值示意

在 Python 中匯入與管理財務資料

變化式:SwarmPlot

sns.swarmplot(x='Sector', y='market_cap_m', data=nasdaq)
plt.xticks(rotation=75)
plt.show()

SwarmPlot 範例

在 Python 中匯入與管理財務資料

一起來練習吧!

在 Python 中匯入與管理財務資料

Preparing Video For Download...