刪除欄位
在實務上,DataFrame 常會包含對分析沒有幫助的欄位。為了讓你更容易專注在其餘欄位,這些欄位應該從 DataFrame 移除。
在這個練習中,你會刪除 county_name 欄,因為它只包含遺漏值;也會刪除 state 欄,因為所有交通攔查都發生在同一個州(Rhode Island)。因此,這兩個欄位不含有用資訊,可以移除。每個欄位的遺漏值數量已為你列出。
本練習屬於課程
使用 pandas 分析警方執法活動
練習說明
- 檢視 DataFrame 的
.shape,以了解列數與欄數。 - 透過將欄名組成字串清單傳給
.drop()方法,一次刪除county_name與state兩個欄位。 - 再次檢視
.shape,確認現在少了 2 個欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Examine the shape of the DataFrame
print(ri.____)
# Drop the 'county_name' and 'state' columns
ri.____([____, ____], axis='columns', inplace=True)
# Examine the shape of the DataFrame (again)
print(____)