開始使用免費開始

依產業與交易所計算多項指標

.agg() 函式能讓你用更多方式彙總資料。提供統計方法名稱的清單時,可以一次計算多個摘要統計量。你也可以用 rename 方法為彙總後的欄位指定新名稱;它接受一個字典做為引數,其中鍵是你計算的指標名稱,值是你想要的新名稱。

在這個練習中,你會計算以百萬美元為單位的市值之平均數、中位數與標準差。pandas 已以 pd 匯入,matplotlib.pyplot 已以 plt 匯入,而且包含參考欄位 'Exchange'listings DataFrame 已經在你的工作環境中可用。

本練習屬於課程

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

檢視課程

練習說明

  • 使用廣播與 .div(),建立新欄位 'market_cap_m',其內容為以百萬美元計的市值。
  • 'Sector''Exchange' 共同分組,將結果指定給 by_sector_exchange
  • 取出 by_sector_exchangemarket_cap_m 欄位,指定給變數 bse_mcm
  • 使用 .agg()market_cap_m 計算平均數、中位數與標準差,並呼叫 rename 方法,於關鍵字參數 columns 傳入字典,將結果分別命名為 'Average''Median''Standard Deviation',並指定給 summary
  • 在主控台列印結果。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create market_cap_m
listings['market_cap_m'] = ____[____].div(1e6)

# Group listing by both Sector and Exchange
by_sector_exchange = ____.____(['Sector', 'Exchange'])

# Subset market_cap_m of by_sector_exchange
bse_mcm = ____[____]

# Calculate mean, median, and std in summary
summary = ____.____(['____', '____', '____']).rename(columns={'mean': ____, 'median': ____, 'std':____})

# Print the summary
print(summary)
編輯並執行程式碼