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.____()))