開始使用免費開始

驗證資料載入

假設你每個月都會收到一個新檔案,你大概知道應該有多少筆記錄與多少欄。在這個練習中,我們要寫一個函式來驗證載入的檔案是否符合預期。

本練習屬於課程

使用 PySpark 進行特徵工程

檢視課程

練習說明

  • 建立資料驗證函式 check_load(),參數為資料框 df、記錄數 num_records,以及欄數 num_columns
  • 使用 num_records 檢查輸入的資料框 dfcount() 計算後,是否有相同的筆數。
  • 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))
編輯並執行程式碼