使用 pandas 分析警方執法活動
Kevin Markham
Founder, Data School
ri.search_conducted.value_counts()
False 83229
True 3307
ri.search_type.value_counts(dropna=False)
NaN 83229
Incident to Arrest 1290
Probable Cause 924
Inventory 219
Reasonable Suspicion 214
Protective Frisk 164
Incident to Arrest,Inventory 123
...
.value_counts() 預設會排除遺漏值dropna=False 會顯示遺漏值ri.search_type.value_counts()
Incident to Arrest 1290
Probable Cause 924
Inventory 219
Reasonable Suspicion 214
Protective Frisk 164
Incident to Arrest,Inventory 123
Incident to Arrest,Probable Cause 100
...
ri['inventory'] = ri.search_type.str.contains('Inventory', na=False)
str.contains() 回傳 True,否則回傳 Falsena=False 將遺漏值視為 Falseri.inventory.dtype
dtype('bool')
True 表示有執行 inventory,False 表示沒有ri.inventory.sum()
441
ri.inventory.mean()
0.0050961449570121106
searched = ri[ri.search_conducted == True]
searched.inventory.mean()
0.13335349259147264
使用 pandas 分析警方執法活動