開始使用免費開始

修正資料型別

在上一個練習中,你看到 is_arrested 欄位目前是 object 資料型別。這個練習中,我們要把它改成 bool,這是最適合包含 TrueFalse 值的欄位型別。

修正資料型別之後,你就能在 is_arrested 欄位上使用原本無法進行的數學運算。

本練習屬於課程

使用 pandas 分析警方執法活動

檢視課程

練習說明

  • 檢視 is_arrested 欄位的前幾列,確認它包含 TrueFalse 值,並查看欄位的資料型別。
  • 使用 .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(____)
編輯並執行程式碼