一对多合并
一家企业可能只有一位所有者,也可能有多位所有者。在本练习中,您将继续通过将企业所有者表 biz_owners 合并到 licenses 表来练习一对多合并。回顾视频课程内容:在一对多关系中,如果左表中的某行关联到右表中的多行,那么该行会在结果中重复。接下来,您将进一步探索这一点,并找出最常见的企业所有者头衔是什么(例如 secretary、CEO 或 vice president)。
licenses 和 biz_owners 这两个 DataFrame 已为您加载完成。
本练习是课程的一部分
使用 pandas 连接数据
练习说明
- 以左侧的
licenses表为起点,在列account上将其与biz_owners表合并,并将结果保存为变量licenses_owners。 - 按
title对licenses_owners分组,并统计每个头衔对应的账户数量。将结果保存为counted_df。 - 按账户数量降序排序
counted_df,并将其保存为变量sorted_df。 - 使用
.head()方法打印sorted_df的前几行。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Merge the licenses and biz_owners table on account
licenses_owners = ____
# Group the results by title then count the number of accounts
counted_df = licenses_owners.groupby(____).agg({'account':'count'})
# Sort the counted_df in descending order
sorted_df = counted_df.sort_values(____)
# Use .head() method to print the first few rows of sorted_df
print(____)