pandas groupby

import pandas as pd
path = 'https://raw.githubusercontent.com/HoijanLai/dataset/master/PoliceKillingsUS.csv'
data = pd.read_csv(path,index_col=None)
data.head()
``            name       date    race  age signs_of_mental_illness  flee
0          Tim Elliot  02/01/15    A   53.0     True              Not fleeing
1    Lewis Lee Lembke  02/01/15    W   47.0    False              Not fleeing
2  John Paul Quintero  03/01/15    H   23.0    False              Not fleeing
3     Matthew Hoffman  04/01/15    W   32.0     True              Not fleeing
4   Michael Rodriguez  04/01/15    H   39.0    False              Not fleeing

###  
data['race'].value_counts()
W    1201
B     618
H     423
A      39
N      31
O      28
###  
data.groupby('race')['age'].mean()
race
A    36.605263
B    31.635468
H    32.995157
N    30.451613
O    33.071429
W    40.046980
###  
data['signs_of_mental_illness'].value_counts()
False    1902
True      633
###  
data.groupby('race')['signs_of_mental_illness'].value_counts()

race  signs_of_mental_illness
A     False                       29
      True                        10
B     False                      523
      True                        95
H     False                      338
      True                        85
N     False                       23
      True                         8
O     False                       21
      True                         7
W     False                      819
      True                       382
data.groupby('race'['signs_of_mental_illness'].value_counts().unstack()

signs_of_mental_illness False True
race
A                         29   10 
B                         523  95 
H                         338  85 
N                         23   8 
O                         21   7 
W                         819  382 
###  
data.groupby('race')['flee'].value_counts().unstack()
flee    Car   Foot  Not fleeing  Other
race                                  
A       4.0    5.0         28.0    NaN
B      90.0  113.0        379.0   23.0
H      74.0   50.0        268.0   19.0
N       2.0    6.0         21.0    NaN
O       4.0    2.0         21.0    1.0
W     191.0  103.0        838.0   46.0
###  
data.groupby('race'['flee'].value_counts().unstack().plot(kind='bar',figsize=(20,5))
https://img-blog.csdnimg.cn/20191121093253192.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIxODQwMjAx,size_16,color_FFFFFF,t_70

좋은 웹페이지 즐겨찾기