驗證資料型別
在這個資料時代,我們能取得的屬性比以往更多。為了處理這些屬性,我們會建立許多自動化流程,但至少要先確保它們的資料型別正確。在本練習中,你會驗證一個屬性及其資料型別的字典,看看是否正確。這個字典已存於變數 validation_dict,並已載入到你的工作環境中。
本練習屬於課程
使用 PySpark 進行特徵工程
練習說明
- 使用
df的dtypes建立由屬性與資料型別組成的 tuple 清單,命名為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.')