{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datascience import *\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "sns.set_palette('husl')\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn import metrics\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "path_data = 'http://www.millerjw.com/dom/mgmt462/pfiles/'\n",
    "crime = Table.read_table(path_data + 'crime.csv')\n",
    "# Make a copy of the usda data in pandas dataframe format \n",
    "crimep = crime.to_df()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can get infromation from a Python dataframe using\n",
    "df.head(), df.describe(), and dr.info().  The crime data is in the data frame named crimep. (Use creimep instead of df).\n",
    "Code three statements in the boxes below to get infromation from the crimep dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Beat</th>\n",
       "      <th>Year</th>\n",
       "      <th>Latitude</th>\n",
       "      <th>Longitude</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.916410e+05</td>\n",
       "      <td>191641.000000</td>\n",
       "      <td>191641.000000</td>\n",
       "      <td>189365.000000</td>\n",
       "      <td>189365.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>4.968629e+06</td>\n",
       "      <td>1258.810771</td>\n",
       "      <td>2006.134940</td>\n",
       "      <td>41.844555</td>\n",
       "      <td>-87.677782</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.342997e+06</td>\n",
       "      <td>702.530465</td>\n",
       "      <td>3.520285</td>\n",
       "      <td>0.085583</td>\n",
       "      <td>0.057333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.310022e+06</td>\n",
       "      <td>111.000000</td>\n",
       "      <td>2001.000000</td>\n",
       "      <td>41.644710</td>\n",
       "      <td>-87.925510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>2.832144e+06</td>\n",
       "      <td>722.000000</td>\n",
       "      <td>2003.000000</td>\n",
       "      <td>41.771618</td>\n",
       "      <td>-87.718474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>4.762956e+06</td>\n",
       "      <td>1121.000000</td>\n",
       "      <td>2006.000000</td>\n",
       "      <td>41.850029</td>\n",
       "      <td>-87.677597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>7.201878e+06</td>\n",
       "      <td>1733.000000</td>\n",
       "      <td>2009.000000</td>\n",
       "      <td>41.917116</td>\n",
       "      <td>-87.635513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>9.181151e+06</td>\n",
       "      <td>2535.000000</td>\n",
       "      <td>2012.000000</td>\n",
       "      <td>42.022878</td>\n",
       "      <td>-87.524676</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 ID           Beat           Year       Latitude  \\\n",
       "count  1.916410e+05  191641.000000  191641.000000  189365.000000   \n",
       "mean   4.968629e+06    1258.810771    2006.134940      41.844555   \n",
       "std    2.342997e+06     702.530465       3.520285       0.085583   \n",
       "min    1.310022e+06     111.000000    2001.000000      41.644710   \n",
       "25%    2.832144e+06     722.000000    2003.000000      41.771618   \n",
       "50%    4.762956e+06    1121.000000    2006.000000      41.850029   \n",
       "75%    7.201878e+06    1733.000000    2009.000000      41.917116   \n",
       "max    9.181151e+06    2535.000000    2012.000000      42.022878   \n",
       "\n",
       "           Longitude  \n",
       "count  189365.000000  \n",
       "mean      -87.677782  \n",
       "std         0.057333  \n",
       "min       -87.925510  \n",
       "25%       -87.718474  \n",
       "50%       -87.677597  \n",
       "75%       -87.635513  \n",
       "max       -87.524676  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crimep.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the code below to see how many crimes had an arrest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15536\n",
      "176105\n",
      "191641\n"
     ]
    }
   ],
   "source": [
    "x = crimep['Arrest'].value_counts()\n",
    "arrested = x[1]\n",
    "notarrested = x[0]\n",
    "totalrows = x[1] + x[0]\n",
    "print(arrested)\n",
    "print(notarrested)\n",
    "print(totalrows)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the cell below, use the variables arrested and total rows to calculate the percentage of arrests.  Remember to multiply by 100 to get a percentage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.106824740008662"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "arrested/totalrows*100\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below to create dataframe y that will contain crime counts by year."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2001    20669\n",
       "2002    18753\n",
       "2004    16862\n",
       "2003    16657\n",
       "2005    16484\n",
       "2006    16098\n",
       "2011    15637\n",
       "2010    15497\n",
       "2008    14445\n",
       "2007    14280\n",
       "2012    14092\n",
       "2009    12167\n",
       "Name: Year, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y = (crimep['Year'].value_counts())\n",
    "display(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use the cell below to run describe on dataframe y.  Just like you ran describe on dataframe crimep earlier."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below and look at the output.  The statement that you will run does the same thing as an Excel Pivot Table using LocationDescription and Arrest in Rows and the count of any column in the values box."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LocationDescription                              Arrest\n",
       "ABANDONED BUILDING                               False          4\n",
       "AIRPORT BUILDING NON-TERMINAL - NON-SECURE AREA  False          4\n",
       "AIRPORT BUILDING NON-TERMINAL - SECURE AREA      False          1\n",
       "AIRPORT EXTERIOR - NON-SECURE AREA               False         18\n",
       "                                                 True           6\n",
       "AIRPORT EXTERIOR - SECURE AREA                   False          1\n",
       "AIRPORT PARKING LOT                              False          8\n",
       "                                                 True           3\n",
       "AIRPORT TERMINAL UPPER LEVEL - NON-SECURE AREA   False          5\n",
       "AIRPORT VENDING ESTABLISHMENT                    False          4\n",
       "                                                 True           6\n",
       "AIRPORT/AIRCRAFT                                 False        267\n",
       "                                                 True          96\n",
       "ALLEY                                            False       2059\n",
       "                                                 True         249\n",
       "ANIMAL HOSPITAL                                  False          1\n",
       "APARTMENT                                        False        157\n",
       "                                                 True          27\n",
       "APPLIANCE STORE                                  False          1\n",
       "ATHLETIC CLUB                                    False          6\n",
       "                                                 True           3\n",
       "BANK                                             False          6\n",
       "                                                 True           1\n",
       "BAR OR TAVERN                                    False         15\n",
       "                                                 True           2\n",
       "BARBERSHOP                                       False          4\n",
       "BOWLING ALLEY                                    False          2\n",
       "                                                 True           1\n",
       "BRIDGE                                           False          2\n",
       "CAR WASH                                         False         37\n",
       "                                                            ...  \n",
       "RESTAURANT                                       True           5\n",
       "SAVINGS AND LOAN                                 False          4\n",
       "SCHOOL, PRIVATE, BUILDING                        False         12\n",
       "                                                 True           2\n",
       "SCHOOL, PRIVATE, GROUNDS                         False         20\n",
       "                                                 True           3\n",
       "SCHOOL, PUBLIC, BUILDING                         False         92\n",
       "                                                 True          22\n",
       "SCHOOL, PUBLIC, GROUNDS                          False        168\n",
       "                                                 True          38\n",
       "SIDEWALK                                         False        419\n",
       "                                                 True          43\n",
       "SMALL RETAIL STORE                               False         26\n",
       "                                                 True           7\n",
       "SPORTS ARENA/STADIUM                             False        161\n",
       "                                                 True           5\n",
       "STREET                                           False     144969\n",
       "                                                 True       11595\n",
       "TAVERN/LIQUOR STORE                              False         12\n",
       "                                                 True           2\n",
       "TAXICAB                                          False         18\n",
       "                                                 True           3\n",
       "VACANT LOT/LAND                                  False        918\n",
       "                                                 True          67\n",
       "VEHICLE NON-COMMERCIAL                           False        758\n",
       "                                                 True          59\n",
       "VEHICLE-COMMERCIAL                               False         22\n",
       "                                                 True           1\n",
       "WAREHOUSE                                        False         16\n",
       "                                                 True           1\n",
       "Name: Arrest, Length: 134, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crimep.groupby(['LocationDescription','Arrest'])['Arrest'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
