分割成多個欄位
你已經透過移除無效列,大幅清理了 DataFrame。現在你想根據 DataFrame 的內容,產生一些有意義的特定欄位,來進一步進行轉換。
你已經有 spark 內容(context)以及最新版的 annotations_df DataFrame。pyspark.sql.functions 以別名 F 可供使用。
本練習屬於課程
使用 PySpark 清理資料
練習說明
- 以定位字元(tab)分割
'_c0'欄位的內容,並將結果儲存在名為split_cols的變數中。 - 依上述變數的前 4 個元素,在名為
split_df的 DataFrame 上新增下列欄位:folder、filename、width、height。 - 將變數
split_cols加成一個欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Split the content of _c0 on the tab character (aka, '\t')
split_cols = ____(annotations_df['____'], '\t')
# Add the columns folder, filename, width, and height
split_df = annotations_df.withColumn('folder', split_cols.getItem(____))
split_df = split_df.withColumn('filename', ____
split_df = split_df.____
____
# Add split_cols as a column
split_df = split_df.____