Path: blob/master/04_Apply/US_Crime_Rates/Solutions.ipynb
548 views
Kernel: Python [default]
United States - Crime Rates - 1960 - 2014
Introduction:
This time you will create a data
Special thanks to: https://github.com/justmarkham for sharing the dataset and materials.
Step 1. Import the necessary libraries
In [95]:
Step 2. Import the dataset from this address.
Step 3. Assign it to a variable called crime.
In [265]:
Out[265]:
Step 4. What is the type of the columns?
In [266]:
Out[266]:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 55 entries, 0 to 54
Data columns (total 12 columns):
Year 55 non-null int64
Population 55 non-null int64
Total 55 non-null int64
Violent 55 non-null int64
Property 55 non-null int64
Murder 55 non-null int64
Forcible_Rape 55 non-null int64
Robbery 55 non-null int64
Aggravated_assault 55 non-null int64
Burglary 55 non-null int64
Larceny_Theft 55 non-null int64
Vehicle_Theft 55 non-null int64
dtypes: int64(12)
memory usage: 5.2 KB
Have you noticed that the type of Year is int64. But pandas has a different type to work with Time Series. Let's see it now.
Step 5. Convert the type of the column Year to datetime64
In [267]:
Out[267]:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 55 entries, 0 to 54
Data columns (total 12 columns):
Year 55 non-null datetime64[ns]
Population 55 non-null int64
Total 55 non-null int64
Violent 55 non-null int64
Property 55 non-null int64
Murder 55 non-null int64
Forcible_Rape 55 non-null int64
Robbery 55 non-null int64
Aggravated_assault 55 non-null int64
Burglary 55 non-null int64
Larceny_Theft 55 non-null int64
Vehicle_Theft 55 non-null int64
dtypes: datetime64[ns](1), int64(11)
memory usage: 5.2 KB
Step 6. Set the Year column as the index of the dataframe
In [268]:
Out[268]:
Step 7. Delete the Total column
In [269]:
Out[269]:
Step 8. Group the year by decades and sum the values
Pay attention to the Population column number, summing this column is a mistake
In [270]:
Out[270]:
Step 9. What is the most dangerous decade to live in the US?
In [276]:
Out[276]:
Population 2010
Violent 1990
Property 1990
Murder 1990
Forcible_Rape 1990
Robbery 1990
Aggravated_assault 1990
Burglary 1980
Larceny_Theft 1990
Vehicle_Theft 1990
dtype: int64