開始使用免費開始

每張影像的計數

你在為這個資料集打造資料管線的下一個任務,是建立幾個以分析為導向的欄位。你被要求根據先前建立的 dog_list 欄位,計算每張影像中找到的狗的數量。你也已經建立了 DogType,可用來更妥善解析某些資料欄位中的內容。

joined_df 會以你上一個定義的狀態提供,同時已定義 DogType 的 StructType。pyspark.sql.functionsF 別名可用。

本練習屬於課程

使用 PySpark 清理資料

檢視課程

練習說明

  • 建立一個 Python 函式,將 dog_list 中的每個項目切割成對應的部分。記得把字串轉成正確的型別,否則 DogType 會無法正確解析。
  • 用上面的函式建立一個 UDF。
  • 使用該 UDF 建立一個名為 dogs 的新欄位。
  • 顯示新欄位中前 10 列的狗的數量。

動手互動練習

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

# Create a function to return the number and type of dogs as a tuple
def dogParse(doglist):
  dogs = []
  for dog in doglist:
    (breed, start_x, start_y, end_x, end_y) = dog.____('____')
    dogs.append((____, int(____), ____, ____, ____))
  return dogs

# Create a UDF
udfDogParse = ____(____, ArrayType(____))

# Use the UDF to list of dogs
joined_df = joined_df.____('____', ____('____'))

# Show the number of dogs in the first 10 rows
joined_df.____(____('____')).____(____)
編輯並執行程式碼