開始使用免費開始

移除含註解的列

你的主管希望你針對一個新的資料集進行較複雜的剖析。這些資料是 ImageNet 資料集的標註資訊,但特別聚焦於犬種,並在影像中辨識牠們。在開始任何實際分析之前,你需要先清掉多個無效或不正確的資料部分。由於文件的整體結構不明,你想先把每一列都匯入到同一個欄位,方便快速檢視與分析。

首先,你需要移除資料集中所有註解列。

已為你提供 spark 內容與基礎 CSV 檔案(annotations.csv.gz)。col 函式也可供使用。

本練習屬於課程

使用 PySpark 清理資料

檢視課程

練習說明

  • annotations.csv.gz 檔案匯入為 DataFrame,並計算列數。請將分隔字元指定為 |
  • 查詢以 # 開頭的列數。
  • 再次將檔案匯入為新的 DataFrame,但在選項中指定註解字元,以移除所有註解列。
  • 對新的 DataFrame 做計數,並確認差異符合預期。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import the file to a DataFrame and perform a row count
annotations_df = spark.read.____('____', sep=____)
full_count = annotations_df.____

# Count the number of rows beginning with '#'
comment_count = annotations_df.____(col('_c0').____('#')).count()

# Import the file to a new DataFrame, without commented rows
no_comments_df = ____.____.____('____', ____=____, comment='____')

# Count the new DataFrame and verify the difference is as expected
no_comments_count = no_comments_df.count()
print("Full count: %d\nComment count: %d\nRemaining count: %d" % (____, ____, ____))
編輯並執行程式碼