Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
guipsamora
GitHub Repository: guipsamora/pandas_exercises
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

Step 2. Import the dataset from this address.

Step 3. Assign it to a variable called crime.

Step 4. What is the type of the columns?

<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

<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

Step 7. Delete the Total column

Step 8. Group the year by decades and sum the values

Pay attention to the Population column number, summing this column is a mistake

Step 9. What is the most dangerous decade to live in the US?

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