PySpark DataFrame 子集與清理
在檢視資料之後,通常需要進行清理,主要包含取子集、重新命名欄位、移除重複列等。PySpark 的 DataFrame API 提供多種運算子來完成這些工作。在本練習中,你需要從 people_df DataFrame 取出「name」、「sex」與「date of birth」這三個欄位,移除資料集中的任何重複列,並分別在移除重複前後計算列數。
請記得,你的工作環境中已提供 SparkSession spark 與 DataFrame people_df。
本練習屬於課程
使用 PySpark 的 Big Data 基礎
練習說明
- 從
people_df選取「name」、「sex」與「date of birth」欄位,並建立people_df_subDataFrame。 - 列印
people_df_subDataFrame 的前 10 筆觀測值。 - 從
people_df_subDataFrame 移除重複列,並建立people_df_sub_nodupDataFrame。 - 在移除重複之前與之後,各有多少列?
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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.____()))