开始使用免费开始使用

一对多合并

一家企业可能只有一位所有者,也可能有多位所有者。在本练习中,您将继续通过将企业所有者表 biz_owners 合并到 licenses 表来练习一对多合并。回顾视频课程内容:在一对多关系中,如果左表中的某行关联到右表中的多行,那么该行会在结果中重复。接下来,您将进一步探索这一点,并找出最常见的企业所有者头衔是什么(例如 secretary、CEO 或 vice president)。

licensesbiz_owners 这两个 DataFrame 已为您加载完成。

本练习是课程的一部分

使用 pandas 连接数据

查看课程

练习说明

  • 以左侧的 licenses 表为起点,在列 account 上将其与 biz_owners 表合并,并将结果保存为变量 licenses_owners
  • titlelicenses_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(____)
编辑并运行代码