開始使用免費開始

合併多個資料表

你現在想嘗試另一個方向,對開球(punt)期間的球員位置進行對應。你或許記得 NextGenStats(NGS)系統會在每一次進攻中,以每秒 10 次的頻率,擷取所有球員的位置與朝向。這可是大量的資料!

你將把三個資料框合併起來,以便進行分析。以下是它們的名稱與說明。

  • games:依 GameKey 的高層級資料
  • punts:依 GameKey 與 PlayId 的單次進攻層級資料
  • ngs:依 GameKey、PlayId、GSISID(球員 ID)與 Time 的位置資料

你的隊友在第 2 行提供了一段串列生成式,能用一行程式碼印出每個資料框的索引。若想深入了解串列生成式,請參考 Python Data Science Toolbox Part 2。

本練習屬於課程

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

檢視課程

練習說明

  • games 為主要資料框,使用索引進行內聯結(inner join)。
  • 檢視合併後資料框的前 10 列。
  • 確保新資料框的索引不含重複值。

動手互動練習

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

# List the index of each data frame
print([[n for n in df.index.names] for df in [games, punts, ngs]])

# Inner join the data frames
games_all = ____.____([punts, ____], how=____)

# View first 10 rows of new frame
print(____.head(10))

# Check index for duplicates
print(____.index.____.sum())
編輯並執行程式碼