执行半连接(semi join)
收入最高的一些曲目来自电视节目或其他非音乐音频。您拿到了一张包含高收入条目的发票表。同时,还有一张来自流媒体服务的非音乐曲目表。在本练习中,您将使用半连接来找出带来最高收入的非音乐曲目。
表 non_mus_tcks、top_invoices 和 genres 已为您加载。
本练习是课程的一部分
使用 pandas 连接数据
练习说明
- 使用内连接在
tid上合并non_mus_tcks和top_invoices。将结果保存为tracks_invoices。 - 使用
.isin()筛选non_mus_tcks中那些tid出现在tracks_invoices的tid列中的行。将结果保存为top_tracks。 - 按
gid对top_tracks分组,并统计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(____)