验证数据类型
在数据时代,您能获取的属性比以往任何时候都多。为处理它们,我们会构建大量自动化流程,但至少要确保它们的数据类型是正确的。本练习将验证一个属性及其数据类型的字典,检查是否正确。该字典保存在变量 validation_dict 中,已在您的工作空间中提供。
本练习是课程的一部分
使用 PySpark 进行特征工程
练习说明
- 使用
df的dtypes创建由属性名和数据类型组成的元组列表,命名为actual_dtypes_list。 - 遍历
actual_dtypes_list,检查列表中的列名是否存在于期望数据类型字典validation_dict中。 - 对于存在于字典中的键,比较其数据类型,并打印出匹配的项。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# create list of actual dtypes to check
actual_dtypes_list = df.____
print(actual_dtypes_list)
# Iterate through the list of actual dtypes tuples
for attribute_tuple in ____:
# Check if column name is dictionary of expected dtypes
col_name = attribute_tuple[____]
if col_name in ____:
# Compare attribute types
col_type = attribute_tuple[____]
if col_type == validation_dict[____]:
print(col_name + ' has expected dtype.')