{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# United States - Crime Rates - 1960 - 2014"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction:\n",
"\n",
"This time you will create a data \n",
"\n",
"Special thanks to: https://github.com/justmarkham for sharing the dataset and materials.\n",
"\n",
"### Step 1. Import the necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2. Import the dataset from this [address](https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/04_Apply/US_Crime_Rates/US_Crime_Rates_1960_2014.csv). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3. Assign it to a variable called crime."
]
},
{
"cell_type": "code",
"execution_count": 265,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" Year | \n",
" Population | \n",
" Total | \n",
" Violent | \n",
" Property | \n",
" Murder | \n",
" Forcible_Rape | \n",
" Robbery | \n",
" Aggravated_assault | \n",
" Burglary | \n",
" Larceny_Theft | \n",
" Vehicle_Theft | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 1960 | \n",
" 179323175 | \n",
" 3384200 | \n",
" 288460 | \n",
" 3095700 | \n",
" 9110 | \n",
" 17190 | \n",
" 107840 | \n",
" 154320 | \n",
" 912100 | \n",
" 1855400 | \n",
" 328200 | \n",
"
\n",
" \n",
" | 1 | \n",
" 1961 | \n",
" 182992000 | \n",
" 3488000 | \n",
" 289390 | \n",
" 3198600 | \n",
" 8740 | \n",
" 17220 | \n",
" 106670 | \n",
" 156760 | \n",
" 949600 | \n",
" 1913000 | \n",
" 336000 | \n",
"
\n",
" \n",
" | 2 | \n",
" 1962 | \n",
" 185771000 | \n",
" 3752200 | \n",
" 301510 | \n",
" 3450700 | \n",
" 8530 | \n",
" 17550 | \n",
" 110860 | \n",
" 164570 | \n",
" 994300 | \n",
" 2089600 | \n",
" 366800 | \n",
"
\n",
" \n",
" | 3 | \n",
" 1963 | \n",
" 188483000 | \n",
" 4109500 | \n",
" 316970 | \n",
" 3792500 | \n",
" 8640 | \n",
" 17650 | \n",
" 116470 | \n",
" 174210 | \n",
" 1086400 | \n",
" 2297800 | \n",
" 408300 | \n",
"
\n",
" \n",
" | 4 | \n",
" 1964 | \n",
" 191141000 | \n",
" 4564600 | \n",
" 364220 | \n",
" 4200400 | \n",
" 9360 | \n",
" 21420 | \n",
" 130390 | \n",
" 203050 | \n",
" 1213200 | \n",
" 2514400 | \n",
" 472800 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Year Population Total Violent Property Murder Forcible_Rape \\\n",
"0 1960 179323175 3384200 288460 3095700 9110 17190 \n",
"1 1961 182992000 3488000 289390 3198600 8740 17220 \n",
"2 1962 185771000 3752200 301510 3450700 8530 17550 \n",
"3 1963 188483000 4109500 316970 3792500 8640 17650 \n",
"4 1964 191141000 4564600 364220 4200400 9360 21420 \n",
"\n",
" Robbery Aggravated_assault Burglary Larceny_Theft Vehicle_Theft \n",
"0 107840 154320 912100 1855400 328200 \n",
"1 106670 156760 949600 1913000 336000 \n",
"2 110860 164570 994300 2089600 366800 \n",
"3 116470 174210 1086400 2297800 408300 \n",
"4 130390 203050 1213200 2514400 472800 "
]
},
"execution_count": 265,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4. What is the type of the columns?"
]
},
{
"cell_type": "code",
"execution_count": 266,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RangeIndex: 55 entries, 0 to 54\n",
"Data columns (total 12 columns):\n",
"Year 55 non-null int64\n",
"Population 55 non-null int64\n",
"Total 55 non-null int64\n",
"Violent 55 non-null int64\n",
"Property 55 non-null int64\n",
"Murder 55 non-null int64\n",
"Forcible_Rape 55 non-null int64\n",
"Robbery 55 non-null int64\n",
"Aggravated_assault 55 non-null int64\n",
"Burglary 55 non-null int64\n",
"Larceny_Theft 55 non-null int64\n",
"Vehicle_Theft 55 non-null int64\n",
"dtypes: int64(12)\n",
"memory usage: 5.2 KB\n"
]
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### 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.\n",
"\n",
"### Step 5. Convert the type of the column Year to datetime64"
]
},
{
"cell_type": "code",
"execution_count": 267,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RangeIndex: 55 entries, 0 to 54\n",
"Data columns (total 12 columns):\n",
"Year 55 non-null datetime64[ns]\n",
"Population 55 non-null int64\n",
"Total 55 non-null int64\n",
"Violent 55 non-null int64\n",
"Property 55 non-null int64\n",
"Murder 55 non-null int64\n",
"Forcible_Rape 55 non-null int64\n",
"Robbery 55 non-null int64\n",
"Aggravated_assault 55 non-null int64\n",
"Burglary 55 non-null int64\n",
"Larceny_Theft 55 non-null int64\n",
"Vehicle_Theft 55 non-null int64\n",
"dtypes: datetime64[ns](1), int64(11)\n",
"memory usage: 5.2 KB\n"
]
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 6. Set the Year column as the index of the dataframe"
]
},
{
"cell_type": "code",
"execution_count": 268,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Population | \n",
" Total | \n",
" Violent | \n",
" Property | \n",
" Murder | \n",
" Forcible_Rape | \n",
" Robbery | \n",
" Aggravated_assault | \n",
" Burglary | \n",
" Larceny_Theft | \n",
" Vehicle_Theft | \n",
"
\n",
" \n",
" | Year | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1960-01-01 | \n",
" 179323175 | \n",
" 3384200 | \n",
" 288460 | \n",
" 3095700 | \n",
" 9110 | \n",
" 17190 | \n",
" 107840 | \n",
" 154320 | \n",
" 912100 | \n",
" 1855400 | \n",
" 328200 | \n",
"
\n",
" \n",
" | 1961-01-01 | \n",
" 182992000 | \n",
" 3488000 | \n",
" 289390 | \n",
" 3198600 | \n",
" 8740 | \n",
" 17220 | \n",
" 106670 | \n",
" 156760 | \n",
" 949600 | \n",
" 1913000 | \n",
" 336000 | \n",
"
\n",
" \n",
" | 1962-01-01 | \n",
" 185771000 | \n",
" 3752200 | \n",
" 301510 | \n",
" 3450700 | \n",
" 8530 | \n",
" 17550 | \n",
" 110860 | \n",
" 164570 | \n",
" 994300 | \n",
" 2089600 | \n",
" 366800 | \n",
"
\n",
" \n",
" | 1963-01-01 | \n",
" 188483000 | \n",
" 4109500 | \n",
" 316970 | \n",
" 3792500 | \n",
" 8640 | \n",
" 17650 | \n",
" 116470 | \n",
" 174210 | \n",
" 1086400 | \n",
" 2297800 | \n",
" 408300 | \n",
"
\n",
" \n",
" | 1964-01-01 | \n",
" 191141000 | \n",
" 4564600 | \n",
" 364220 | \n",
" 4200400 | \n",
" 9360 | \n",
" 21420 | \n",
" 130390 | \n",
" 203050 | \n",
" 1213200 | \n",
" 2514400 | \n",
" 472800 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Population Total Violent Property Murder Forcible_Rape \\\n",
"Year \n",
"1960-01-01 179323175 3384200 288460 3095700 9110 17190 \n",
"1961-01-01 182992000 3488000 289390 3198600 8740 17220 \n",
"1962-01-01 185771000 3752200 301510 3450700 8530 17550 \n",
"1963-01-01 188483000 4109500 316970 3792500 8640 17650 \n",
"1964-01-01 191141000 4564600 364220 4200400 9360 21420 \n",
"\n",
" Robbery Aggravated_assault Burglary Larceny_Theft \\\n",
"Year \n",
"1960-01-01 107840 154320 912100 1855400 \n",
"1961-01-01 106670 156760 949600 1913000 \n",
"1962-01-01 110860 164570 994300 2089600 \n",
"1963-01-01 116470 174210 1086400 2297800 \n",
"1964-01-01 130390 203050 1213200 2514400 \n",
"\n",
" Vehicle_Theft \n",
"Year \n",
"1960-01-01 328200 \n",
"1961-01-01 336000 \n",
"1962-01-01 366800 \n",
"1963-01-01 408300 \n",
"1964-01-01 472800 "
]
},
"execution_count": 268,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 7. Delete the Total column"
]
},
{
"cell_type": "code",
"execution_count": 269,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Population | \n",
" Violent | \n",
" Property | \n",
" Murder | \n",
" Forcible_Rape | \n",
" Robbery | \n",
" Aggravated_assault | \n",
" Burglary | \n",
" Larceny_Theft | \n",
" Vehicle_Theft | \n",
"
\n",
" \n",
" | Year | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1960-01-01 | \n",
" 179323175 | \n",
" 288460 | \n",
" 3095700 | \n",
" 9110 | \n",
" 17190 | \n",
" 107840 | \n",
" 154320 | \n",
" 912100 | \n",
" 1855400 | \n",
" 328200 | \n",
"
\n",
" \n",
" | 1961-01-01 | \n",
" 182992000 | \n",
" 289390 | \n",
" 3198600 | \n",
" 8740 | \n",
" 17220 | \n",
" 106670 | \n",
" 156760 | \n",
" 949600 | \n",
" 1913000 | \n",
" 336000 | \n",
"
\n",
" \n",
" | 1962-01-01 | \n",
" 185771000 | \n",
" 301510 | \n",
" 3450700 | \n",
" 8530 | \n",
" 17550 | \n",
" 110860 | \n",
" 164570 | \n",
" 994300 | \n",
" 2089600 | \n",
" 366800 | \n",
"
\n",
" \n",
" | 1963-01-01 | \n",
" 188483000 | \n",
" 316970 | \n",
" 3792500 | \n",
" 8640 | \n",
" 17650 | \n",
" 116470 | \n",
" 174210 | \n",
" 1086400 | \n",
" 2297800 | \n",
" 408300 | \n",
"
\n",
" \n",
" | 1964-01-01 | \n",
" 191141000 | \n",
" 364220 | \n",
" 4200400 | \n",
" 9360 | \n",
" 21420 | \n",
" 130390 | \n",
" 203050 | \n",
" 1213200 | \n",
" 2514400 | \n",
" 472800 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Population Violent Property Murder Forcible_Rape Robbery \\\n",
"Year \n",
"1960-01-01 179323175 288460 3095700 9110 17190 107840 \n",
"1961-01-01 182992000 289390 3198600 8740 17220 106670 \n",
"1962-01-01 185771000 301510 3450700 8530 17550 110860 \n",
"1963-01-01 188483000 316970 3792500 8640 17650 116470 \n",
"1964-01-01 191141000 364220 4200400 9360 21420 130390 \n",
"\n",
" Aggravated_assault Burglary Larceny_Theft Vehicle_Theft \n",
"Year \n",
"1960-01-01 154320 912100 1855400 328200 \n",
"1961-01-01 156760 949600 1913000 336000 \n",
"1962-01-01 164570 994300 2089600 366800 \n",
"1963-01-01 174210 1086400 2297800 408300 \n",
"1964-01-01 203050 1213200 2514400 472800 "
]
},
"execution_count": 269,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 8. Group the year by decades and sum the values\n",
"\n",
"#### Pay attention to the Population column number, summing this column is a mistake"
]
},
{
"cell_type": "code",
"execution_count": 270,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Population | \n",
" Violent | \n",
" Property | \n",
" Murder | \n",
" Forcible_Rape | \n",
" Robbery | \n",
" Aggravated_assault | \n",
" Burglary | \n",
" Larceny_Theft | \n",
" Vehicle_Theft | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1960 | \n",
" 201385000 | \n",
" 4134930 | \n",
" 45160900 | \n",
" 106180 | \n",
" 236720 | \n",
" 1633510 | \n",
" 2158520 | \n",
" 13321100 | \n",
" 26547700 | \n",
" 5292100 | \n",
"
\n",
" \n",
" | 1970 | \n",
" 220099000 | \n",
" 9607930 | \n",
" 91383800 | \n",
" 192230 | \n",
" 554570 | \n",
" 4159020 | \n",
" 4702120 | \n",
" 28486000 | \n",
" 53157800 | \n",
" 9739900 | \n",
"
\n",
" \n",
" | 1980 | \n",
" 248239000 | \n",
" 14074328 | \n",
" 117048900 | \n",
" 206439 | \n",
" 865639 | \n",
" 5383109 | \n",
" 7619130 | \n",
" 33073494 | \n",
" 72040253 | \n",
" 11935411 | \n",
"
\n",
" \n",
" | 1990 | \n",
" 272690813 | \n",
" 17527048 | \n",
" 119053499 | \n",
" 211664 | \n",
" 998827 | \n",
" 5748930 | \n",
" 10568963 | \n",
" 26750015 | \n",
" 77679366 | \n",
" 14624418 | \n",
"
\n",
" \n",
" | 2000 | \n",
" 307006550 | \n",
" 13968056 | \n",
" 100944369 | \n",
" 163068 | \n",
" 922499 | \n",
" 4230366 | \n",
" 8652124 | \n",
" 21565176 | \n",
" 67970291 | \n",
" 11412834 | \n",
"
\n",
" \n",
" | 2010 | \n",
" 318857056 | \n",
" 6072017 | \n",
" 44095950 | \n",
" 72867 | \n",
" 421059 | \n",
" 1749809 | \n",
" 3764142 | \n",
" 10125170 | \n",
" 30401698 | \n",
" 3569080 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Population Violent Property Murder Forcible_Rape Robbery \\\n",
"1960 201385000 4134930 45160900 106180 236720 1633510 \n",
"1970 220099000 9607930 91383800 192230 554570 4159020 \n",
"1980 248239000 14074328 117048900 206439 865639 5383109 \n",
"1990 272690813 17527048 119053499 211664 998827 5748930 \n",
"2000 307006550 13968056 100944369 163068 922499 4230366 \n",
"2010 318857056 6072017 44095950 72867 421059 1749809 \n",
"\n",
" Aggravated_assault Burglary Larceny_Theft Vehicle_Theft \n",
"1960 2158520 13321100 26547700 5292100 \n",
"1970 4702120 28486000 53157800 9739900 \n",
"1980 7619130 33073494 72040253 11935411 \n",
"1990 10568963 26750015 77679366 14624418 \n",
"2000 8652124 21565176 67970291 11412834 \n",
"2010 3764142 10125170 30401698 3569080 "
]
},
"execution_count": 270,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 9. What is the mos dangerous decade to live in the US?"
]
},
{
"cell_type": "code",
"execution_count": 276,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Population 2010\n",
"Violent 1990\n",
"Property 1990\n",
"Murder 1990\n",
"Forcible_Rape 1990\n",
"Robbery 1990\n",
"Aggravated_assault 1990\n",
"Burglary 1980\n",
"Larceny_Theft 1990\n",
"Vehicle_Theft 1990\n",
"dtype: int64"
]
},
"execution_count": 276,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}