開始使用免費開始

篩選你的 DataFrame

在前一個練習中,你使用 select() 運算子做了子集化,這主要用於以欄為單位選取 DataFrame。那如果你想根據條件來子集化 DataFrame 呢(例如,只選取 sex 為 Female 的所有列)?在本練習中,你會在 people_df DataFrame 中,將 'sex' 為 female 與 male 的列分別篩選出來,建立兩個不同的資料集。最後,你會計算這兩個資料集各自的列數。

請記住,你的工作環境中已經有 SparkSession spark 與 DataFrame people_df 可用。

本練習屬於課程

使用 PySpark 的 Big Data 基礎

檢視課程

練習說明

  • people_df DataFrame 中 sex 為 female 的所有列篩選出來,存成 people_df_female DataFrame。
  • people_df DataFrame 中 sex 為 male 的所有列篩選出來,存成 people_df_male DataFrame。
  • 分別計算 people_df_femalepeople_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.____()))
編輯並執行程式碼