开始使用免费开始使用

不同分区下的 ID

您刚刚完成了为一个 DataFrame 添加 ID 字段。现在,请看看当对拥有不同分区数量的 DataFrame 执行相同操作时会发生什么。

要检查分区数量,请在 DataFrame 上使用 .rdd.getNumPartitions() 方法。

工作环境中已提供 spark 会话和两个 DataFrame:voter_dfvoter_df_single。按照说明操作,您将发现这两个 DataFrame 之间的差异。pyspark.sql.functions 库已用别名 F 导入可用。

本练习是课程的一部分

使用 PySpark 进行数据清洗

查看课程

练习说明

  • 打印每个 DataFrame 的分区数量。
  • 为每个 DataFrame 添加一个 ROW_ID 字段。
  • 展示每个 DataFrame 中前 10 个 ID。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Print the number of partitions in each DataFrame
print("\nThere are %d partitions in the voter_df DataFrame.\n" % ____)
print("\nThere are %d partitions in the voter_df_single DataFrame.\n" % ____)

# Add a ROW_ID field to each DataFrame
voter_df = voter_df.____('ROW_ID', ____)
voter_df_single = ____

# Show the top 10 IDs in each DataFrame 
voter_df.____(voter_df.____.desc()).show(____)
____.orderBy(____).show(10)
编辑并运行代码