略過不良資料
在這個練習中,你會使用 read_csv() 的參數來處理含有不良資料的檔案,例如某些紀錄的欄位數量多於資料行數。預設情況下,嘗試匯入這類檔案會觸發特定錯誤:pandas.errors.ParserError。
這裡的 Vermont 稅務資料有些列已經毀損。為了載入正常的列,我們需要告訴 pandas 跳過錯誤。同時,我們也希望在跳過某一列時,pandas 能提出警告,讓我們知道資料問題的範圍。
pandas 已經以 pd 匯入。練習的程式碼會嘗試讀取檔案。若發生 pandas.errors.ParserError,except 區塊中的程式碼就會執行。
本練習屬於課程
使用 pandas 的精實資料導入
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
try:
# Import the CSV without any keyword arguments
data = ____
# View first 5 records
print(data.head())
except pd.errors.ParserError:
print("Your data contained rows that could not be parsed.")