开始使用免费开始使用

用于筛选表的 SQL 查询

在上一个练习中,您已经在 DataFrame 上运行了一条简单的 SQL 查询。您还可以构造更复杂的查询,以获得所需结果,并将其用于后续分析,例如数据可视化和机器学习。在本练习中,我们将使用您之前创建的临时表 people,按性别筛选出 "sex" 为 male 和 female 的行,并分别创建两个 DataFrame。

请注意,"solution" 对 SQL 命令大小写敏感(例如,只接受 FROM,不接受 from)。"solution" 只接受 "==",不接受 "="。

请记住,您的工作区中已经提供了 SparkSession spark 和临时表 people

本练习是课程的一部分

使用 PySpark 的大数据基础

查看课程

练习说明

  • people 表中 sex 为 female 的所有行筛选到 people_female_df DataFrame。
  • people 表中 sex 为 male 的所有行筛选到 people_male_df DataFrame。
  • 分别统计 people_femalepeople_male 两个 DataFrame 的行数。

交互式实操练习

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

# Filter the people table to select female sex 
people_female_df = spark.____('SELECT * FROM ____ WHERE sex=="____"')

# Filter the people table DataFrame to select male sex
people_male_df = spark.____('SELECT * ____ people ____ ____=="____"')

# Count the number of rows in both people_df_female and people_male_df DataFrames
print("There are {} rows in the people_female_df and {} rows in the people_male_df DataFrames".format(people_female_df.____(), people_male_df.____()))
编辑并运行代码