用簡潔程式碼完成複雜合併
好消息!你可以使用聯盟的 Next Gen Stats(NGS)資料。NGS 會在每一次進攻中,記錄每位球員的位置與朝向。資料以每秒 10 次的頻率記錄,這表示光是棄踢(punt)每週就有超過 1,500,000 筆觀測!資料已載入名為 coords 的資料框。
你同時也有每一次棄踢的一般進攻資料,且與 NGS 追蹤到的棄踢相對應。這個名為 punts 的資料框,以 GameKey 與 PlayId 的唯一組合來識別每一列。
若在試算表環境進行合併,你會在每個資料表中建立一個新欄位,將 GameKey 與 PlayId 串接起來,並依此新欄位來對表。現在你可以嘗試用一個簡單的 merge 陳述式,把 punts 與 coords 合併起來。
本練習屬於課程
給試算表使用者的 pandas 合併實務
練習說明
- 檢視
punts的前 10 列。注意每一列都是GameKey-PlayId組合唯一對應。 - 檢視
coords的前 10 列。 - 將兩個資料框合併,
punts作為左側資料框、coords作為右側資料框。 - 檢視新資料框
punts_w_coords的前 15 列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# View punts
print(____.head(10))
# View coords
print(____.head(10))
# Merge data frames
punts_w_coords = ____.merge(____)
# View new data frame
print(____.head(15))