开始使用免费开始使用

验证数据加载

假设您每个月都会收到一个新文件。您预计会有特定的记录数和列数。本练习将编写一个函数,用于验证已加载的文件。

本练习是课程的一部分

使用 PySpark 进行特征工程

查看课程

练习说明

  • 创建数据验证函数 check_load(),其参数包括:数据框 df、记录数 num_records、列数 num_columns
  • 使用 num_records 检查输入数据框 df 通过 count() 得到的记录数是否一致。
  • 使用 columns 上的 len(),将输入数据框的列数与 num_columns 进行比较。
  • 如果两项检查均为 True,则打印 Validation Passed

交互式实操练习

通过完成这段示例代码来试试这个练习。

def ____(____, ____, ____):
  # Takes a dataframe and compares record and column counts to input
  # Message to return if the critera below aren't met
  message = 'Validation Failed'
  # Check number of records
  if num_records == df.____():
    # Check number of columns
    if num_columns == ____(df.____):
      # Success message
      message = ____
  return message

# Print the data validation message
print(check_load(df, 5000, 74))
编辑并运行代码