筛选您的 DataFrame
在上一个练习中,您使用 select() 运算符按列对 DataFrame 进行了子集选择。它主要用于按列筛选。如果您想基于条件对子集进行选择怎么办(例如,选择 sex 为 Female 的所有行)?在本练习中,您将对 people_df DataFrame 进行行筛选,分别选出 'sex' 为 female 和为 male 的记录,并创建两个不同的数据集。最后,您将统计每个数据集的行数。
请注意,工作区中已经为您提供了 SparkSession spark 和 DataFrame people_df。
本练习是课程的一部分
使用 PySpark 的大数据基础
练习说明
- 将
people_dfDataFrame 中 sex 为 female 的所有行筛选到people_df_femaleDataFrame。 - 将
people_dfDataFrame 中 sex 为 male 的所有行筛选到people_df_maleDataFrame。 - 统计
people_df_female和people_df_male两个 DataFrame 的行数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Filter people_df to select females
people_df_female = people_df.____(people_df.____ == "female")
# Filter people_df to select males
people_df_male = people_df.____(____ == "____")
# Count the number of rows
print("There are {} rows in the people_df_female DataFrame and {} rows in the people_df_male DataFrame".format(people_df_female.____(), people_df_male.____()))