執行 semi join
某些帶來最高收入的曲目來自電視節目,或是其他非音樂類的音訊。你拿到了一張包含最高收入品項的發票表,另外還有一張串流服務中的非音樂曲目表。這題你會用 semi join 找出最高收入的非音樂曲目。
資料表 non_mus_tcks、top_invoices 和 genres 已為你載入。
本練習屬於課程
使用 pandas 進行資料表連接
練習說明
- 以內連接將
non_mus_tcks與top_invoices依tid合併。將結果存為tracks_invoices。 - 使用
.isin()篩選出non_mus_tcks中tid存在於tracks_invoices的tid欄位者。將結果存為top_tracks。 - 將
top_tracks依gid分組,並計算tid的筆數。將結果存為cnt_by_gid。 - 將
cnt_by_gid與genres表依gid合併,並印出結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Merge the non_mus_tcks and top_invoices tables on tid
tracks_invoices = ____.merge(____)
# Use .isin() to subset non_mus_tcks to rows with tid in tracks_invoices
top_tracks = _____[non_mus_tcks['tid'].isin(____)]
# Group the top_tracks by gid and count the tid rows
cnt_by_gid = top_tracks.groupby(['gid'], as_index=False).agg({'tid':____})
# Merge the genres table to cnt_by_gid on gid and print
print(____)