验证数据加载
假设您每个月都会收到一个新文件。您预计会有特定的记录数和列数。本练习将编写一个函数,用于验证已加载的文件。
本练习是课程的一部分
使用 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))