移除無效列
你已成功移除以註解開頭的列,現在你拿到了資料的一般格式說明。DataFrame 中至少應該有 5 個以 tab 分隔的欄位。記得你原本的 DataFrame 只有一個欄位,所以你需要用 tab(\t)字元來分割資料。
annotations_df DataFrame 已可使用,且已移除註解列。spark.sql.functions 已以別名 F 匯入。DataFrame 的初始列數已儲存在變數 initial_count 中。
本練習屬於課程
使用 PySpark 清理資料
練習說明
- 使用
annotations_df的'_c0'欄位,依 tab 字元分割後建立新變數tmp_fields。 - 在
annotations_df中建立名為'colcount'的新欄位,代表前一步所得到欄位的數量。 - 篩除
annotations_df中欄位數少於 5 的任何列。 - 計算 DataFrame 的列數,並與
initial_count比較。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Split _c0 on the tab character and store the list in a variable
tmp_fields = ____(annotations_df['_c0'], ____)
# Create the colcount column on the DataFrame
annotations_df = annotations_df.____('____', ____(____))
# Remove any rows containing fewer than 5 fields
annotations_df_filtered = annotations_df.____(~ (____))
# Count the number of rows
final_count = ____
print("Initial count: %d\nFinal count: %d" % (initial_count, final_count))