开始使用免费开始使用

PySpark DataFrame 子集与清洗

在完成数据检查后,通常需要进行数据清洗,主要包括抽取子集、重命名列、删除重复行等。PySpark DataFrame API 提供了多种算子来完成这些操作。本练习中,您的任务是从 people_df DataFrame 中抽取 "name"、"sex" 和 "date of birth" 三列,移除该数据集中的所有重复行,并在去重前后分别统计行数。

请记住,您的工作区中已经提供 SparkSession spark 和 DataFrame people_df

本练习是课程的一部分

使用 PySpark 的大数据基础

查看课程

练习说明

  • people_df 中选择 "name"、"sex" 和 "date of birth" 列,创建 people_df_sub DataFrame。
  • 打印 people_df_sub DataFrame 的前 10 条记录。
  • people_df_sub DataFrame 中移除重复行,并创建 people_df_sub_nodup DataFrame。
  • 去重前后分别有多少行?

交互式实操练习

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

# Select name, sex and date of birth columns
people_df_sub = people_df.____('name', ____, ____)

# Print the first 10 observations from people_df_sub
people_df_sub.____(____)

# Remove duplicate entries from people_df_sub
people_df_sub_nodup = people_df_sub.____()

# Count the number of rows
print("There were {} rows before removing duplicates, and {} rows after removing duplicates".format(people_df_sub.____(), people_df_sub_nodup.____()))
编辑并运行代码