計算保護性搜身次數
在搜查車輛時,警方可能會先對駕駛進行拍搜,以確認是否攜帶武器。這稱為「保護性搜身」。
在這個練習中,你會先檢查「Protective Frisk」是否曾作為唯一的搜查類型,並計算其次數。接著,你會使用字串方法找出所有對駕駛進行搜身的情況。
本練習屬於課程
使用 pandas 分析警方執法活動
練習說明
- 對
ri的search_type欄位進行計數,查看「Protective Frisk」作為唯一搜查類型出現了多少次。 - 建立新欄位
frisk,當search_type包含字串「Protective Frisk」時為True,否則為False。 - 檢查
frisk的資料型別,確認它是布林 Series。 - 對
frisk取總和,以計算搜身的總次數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Count the 'search_type' values
print(____)
# Check if 'search_type' contains the string 'Protective Frisk'
ri['frisk'] = ri.search_type.str.contains('____', na=____)
# Check the data type of 'frisk'
print(____)
# Take the sum of 'frisk'
print(____)