開始使用免費開始

一對多合併

一家公司可能有 1 位或多位負責人。本題將延續一對多合併的練習:把代表公司負責人的資料表 biz_owners,與 licenses 資料表進行合併。回想影片課程中的說明,在一對多關係中,若左表的一列對應到右表的多列,左表該列會在結果中重複出現。這一題會更進一步探索這點,找出最常見的公司負責人職稱(例如:secretary、CEO、vice president)。

licensesbiz_owners 這兩個 DataFrame 已為你載入。

本練習屬於課程

使用 pandas 進行資料表連接

檢視課程

練習說明

  • 以左側的 licenses 資料表為起點,依 account 欄位將其與 biz_owners 合併,並將結果儲存為變數 licenses_owners
  • titlelicenses_owners 分組,計算每個職稱所對應的帳號數量。將結果儲存為 counted_df
  • 依「帳號」的數量「由大到小」排序 counted_df,並將其儲存為變數 sorted_df
  • 使用 .head() 方法列印 sorted_df 的前幾列。

動手互動練習

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

# Merge the licenses and biz_owners table on account
licenses_owners = ____

# Group the results by title then count the number of accounts
counted_df = licenses_owners.groupby(____).agg({'account':'count'})

# Sort the counted_df in descending order
sorted_df = counted_df.sort_values(____)

# Use .head() method to print the first few rows of sorted_df
print(____)
編輯並執行程式碼