如何使用交叉表汇总年度犯罪统计?

2024-05-16 02:42:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在对芝加哥的犯罪数据进行回归分析,我想对每个地区的年度犯罪数量进行汇总。我知道芝加哥的犯罪数据相当大,但可以公开获取以了解数据。现在我所做的是5年内每个地区的总犯罪率,但我只是想看看每个地区的年犯罪率,我只对前5名犯罪感兴趣。你知道吗

数据

这是关于凯格尔的芝加哥犯罪数据:chicago crime data

我所做的:

crimes_2012 = pd.read_csv('Chicago_Crimes_2012_to_2017.csv', sep=',', error_bad_lines=False)
crimes_2012.drop_duplicates(subset=['ID', 'Case Number', 'Date'], inplace=True)
crimes_2012.drop(['Unnamed: 0','Case Number','IUCR','FBI Code','Updated On',
                  'X Coordinate','Y Coordinate'], inplace = True, axis = 1)
crimes_2012 = crimes_2012.dropna(axis = 0, how = 'any')

以下是我对每个地区的犯罪总数所做的计算:

df=crimes_2012[['Primary Type', 'Location Description', 'Community Area']]
crime_catg = df.groupby(['Community Name', 'Primary Type'])['Primary Type'].count().unstack()
crime_catg = crime_catg[['THEFT','BATTERY', 'CRIMINAL DAMAGE', 'NARCOTICS', 'ASSAULT']]

所需输出:

我想得到每个地区/社区每种犯罪类型的年度计数。像这样:

enter image description here

有什么方便的方法可以轻松地做到这一点吗?我尝试了熊猫的交叉表,但实际上没有得到正确的输出。怎么做?你知道吗


Tags: csv数据truenumbertype地区dropcase
1条回答
网友
1楼 · 发布于 2024-05-16 02:42:00

下面是如何创建所需的交叉表。有两个问题,首先需要将列'Date'转换为datetime类型。然后我们将过滤crimes_2012框架的一个子集,以便只包含您感兴趣的^{}^{}5个犯罪。你知道吗

最后,创建^{}并使用^{}获得所需的形状。你知道吗

crimes_2012['Date'] = pd.to_datetime(crimes_2012['Date'], format='%m/%d/%Y %H:%M:%S %p')

top_5_crimes = ['THEFT','BATTERY', 'CRIMINAL DAMAGE', 'NARCOTICS', 'ASSAULT']

df = crimes_2012[crimes_2012['Primary Type'].isin(top_5_crimes)]

df_cross = (pd.crosstab(index=df['Community Area'],
                        columns=[df['Date'].dt.year, df['Primary Type']])
            .sort_index(axis=1, level=[1, 0]))

如果需要展平列级别,请使用:

df_cross.columns = ['{} {}'.format(crime, year) for year, crime in df_cross.columns]

[输出]

print(df_cross.head())

                ASSAULT 2012  ASSAULT 2013  ASSAULT 2014  ASSAULT 2015  \
Community Area                                                           
0.0                        0             0             0             0   
1.0                      340           303           257           234   
2.0                      254           225           201           166   
3.0                      244           277           210           233   
4.0                      124           111            85            99   

                ASSAULT 2016  BATTERY 2012  BATTERY 2013  BATTERY 2014  \
Community Area                                                           
0.0                        0             0             0             0   
1.0                      227           991           866           776   
2.0                      198           658           669           574   
3.0                      241           736           667           593   
4.0                       93           354           352           319   

                BATTERY 2015  BATTERY 2016  ...  NARCOTICS 2012  \
Community Area                              ...                   
0.0                        0             0  ...               0   
1.0                      666           724  ...             485   
2.0                      544           534  ...             230   
3.0                      661           653  ...             735   
4.0                      288           288  ...             111   

                NARCOTICS 2013  NARCOTICS 2014  NARCOTICS 2015  \
Community Area                                                   
0.0                          0               0               0   
1.0                        362             278             205   
2.0                        216             173             157   
3.0                        482             519             271   
4.0                         61             102              78   

                NARCOTICS 2016  THEFT 2012  THEFT 2013  THEFT 2014  \
Community Area                                                       
0.0                          0           0           0           0   
1.0                         79        1043        1004         811   
2.0                         79         976         991         794   
3.0                        100        1338        1134         952   
4.0                         36         691         689         507   

                THEFT 2015  THEFT 2016  
Community Area                          
0.0                      0           0  
1.0                    845         851  
2.0                    669         694  
3.0                    879         968  
4.0                    499         514  

相关问题 更多 >