每张图片计数
在为该数据集构建数据管道的下一步中,您需要创建几个面向分析的列。要求您基于之前创建的 dog_list 列,计算每张图片中检测到的狗的数量。您还创建了 DogType,用于更好地解析部分数据列中的数据。
joined_df 按您上一次定义的形式可用,并且已定义 DogType 的 StructType。pyspark.sql.functions 已以 F 别名导入可用。
本练习是课程的一部分
使用 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.____(____('____')).____(____)