修正資料型別
在上一個練習中,你看到 is_arrested 欄位目前是 object 資料型別。這個練習中,我們要把它改成 bool,這是最適合包含 True 和 False 值的欄位型別。
修正資料型別之後,你就能在 is_arrested 欄位上使用原本無法進行的數學運算。
本練習屬於課程
使用 pandas 分析警方執法活動
練習說明
- 檢視
is_arrested欄位的前幾列,確認它包含True和False值,並查看欄位的資料型別。 - 使用
.astype()方法把is_arrested轉為bool欄位。 - 再次檢查
is_arrested的新資料型別,確認它已是bool欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Examine the head of the 'is_arrested' column
print(ri.is_arrested.____)
# Change the data type of 'is_arrested' to 'bool'
ri['is_arrested'] = ri.is_arrested.____
# Check the data type of 'is_arrested'
print(____)