开始使用免费开始使用

移除无效行

在成功移除注释行之后,您已经拿到关于数据通用格式的一些信息。DataFrame 中至少应包含 5 个以制表符分隔的列。请记住,最初的 DataFrame 只有 1 列,因此您需要按制表符(\t)来拆分数据。

DataFrame annotations_df 已可用,且注释行已被移除。spark.sql.functions 库已通过别名 F 提供。DataFrame 中初始的行数已存储在变量 initial_count 中。

本练习是课程的一部分

使用 PySpark 进行数据清洗

查看课程

练习说明

  • 使用 annotations_df 的列 '_c0',按制表符拆分,创建新变量 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))
编辑并运行代码