驗證資料載入
假設你每個月都會收到一個新檔案,你大概知道應該有多少筆記錄與多少欄。在這個練習中,我們要寫一個函式來驗證載入的檔案是否符合預期。
本練習屬於課程
使用 PySpark 進行特徵工程
練習說明
- 建立資料驗證函式
check_load(),參數為資料框df、記錄數num_records,以及欄數num_columns。 - 使用
num_records檢查輸入的資料框df以count()計算後,是否有相同的筆數。 - 以
len()作用在columns上,將輸入資料框的欄數與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))