開始使用免費開始

完成一對多合併

辛苦的準備工作告一段落,現在該把這些資料表合併起來。你將把 game_matchupspunts 連接在一起。你或許還記得,先前你已經確認這些資料框都準備妥當。接下來只要快速瀏覽一下喚醒記憶,然後撰寫程式碼即可。

合併資料之後,我們可以透過依 GameKey 分組,並計算 PlayId 欄中的筆數,來找出每場比賽發生特定次數棄踢的場數。相關程式碼已為你提供。

本練習屬於課程

給試算表使用者的 pandas 合併實務

檢視課程

練習說明

  • 檢視每個資料框的前 5 列。
  • punts 作為右側資料框進行 inner 合併,並檢視結果

動手互動練習

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

# View first 5 rows of each data frame
print(____.head(), '\n', ____.head())

# Merge data frames 
games_all = ____.merge(____, how='____')
print(____.head(10))

# Produce counts of games by number of punts
counts = games_all.groupby('GameKey')['PlayId'].size()
counts.hist() 
plt.xlabel("Punts per Game")
plt.ylabel("Number of Games")
plt.show()
編輯並執行程式碼