{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: folium in /anaconda3/lib/python3.6/site-packages (0.6.0)\n", "Requirement already satisfied: numpy in /anaconda3/lib/python3.6/site-packages (from folium) (1.14.3)\n", "Requirement already satisfied: six in /anaconda3/lib/python3.6/site-packages (from folium) (1.11.0)\n", "Requirement already satisfied: jinja2 in /anaconda3/lib/python3.6/site-packages (from folium) (2.10)\n", "Requirement already satisfied: requests in /anaconda3/lib/python3.6/site-packages (from folium) (2.18.4)\n", "Requirement already satisfied: branca>=0.3.0 in /anaconda3/lib/python3.6/site-packages (from folium) (0.3.0)\n", "Requirement already satisfied: MarkupSafe>=0.23 in /anaconda3/lib/python3.6/site-packages (from jinja2->folium) (1.0)\n", "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /anaconda3/lib/python3.6/site-packages (from requests->folium) (3.0.4)\n", "Requirement already satisfied: idna<2.7,>=2.5 in /anaconda3/lib/python3.6/site-packages (from requests->folium) (2.6)\n", "Requirement already satisfied: urllib3<1.23,>=1.21.1 in /anaconda3/lib/python3.6/site-packages (from requests->folium) (1.22)\n", "Requirement already satisfied: certifi>=2017.4.17 in /anaconda3/lib/python3.6/site-packages (from requests->folium) (2018.4.16)\n", "\u001b[31mdistributed 1.21.8 requires msgpack, which is not installed.\u001b[0m\n", "\u001b[33mYou are using pip version 10.0.1, however version 18.0 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n", "Requirement already satisfied: numpy in /anaconda3/lib/python3.6/site-packages (1.14.3)\n", "\u001b[31mdistributed 1.21.8 requires msgpack, which is not installed.\u001b[0m\n", "\u001b[33mYou are using pip version 10.0.1, however version 18.0 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n", "Requirement already satisfied: requests in /anaconda3/lib/python3.6/site-packages (2.18.4)\n", "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /anaconda3/lib/python3.6/site-packages (from requests) (3.0.4)\n", "Requirement already satisfied: idna<2.7,>=2.5 in /anaconda3/lib/python3.6/site-packages (from requests) (2.6)\n", "Requirement already satisfied: urllib3<1.23,>=1.21.1 in /anaconda3/lib/python3.6/site-packages (from requests) (1.22)\n", "Requirement already satisfied: certifi>=2017.4.17 in /anaconda3/lib/python3.6/site-packages (from requests) (2018.4.16)\n", "\u001b[31mdistributed 1.21.8 requires msgpack, which is not installed.\u001b[0m\n", "\u001b[33mYou are using pip version 10.0.1, however version 18.0 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" ] } ], "source": [ "import sys\n", "\n", "!{sys.executable} -m pip install folium\n", "!{sys.executable} -m pip install numpy\n", "!{sys.executable} -m pip install requests\n", "import numpy as np\n", "import pandas as pd \n", "import folium \n", "import requests" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def get_and_format_trip_data(file_name):\n", " '''\n", " input: file name\n", " output: pandas data frame\n", " '''\n", " bike_trips = pd.read_csv(file_name)\n", "\n", " bike_trips[\"start_time\"] = pd.to_datetime(bike_trips[\"start_time\"])\n", " bike_trips[\"end_time\"] = pd.to_datetime(bike_trips[\"end_time\"])\n", " bike_trips[\"hour\"] = bike_trips[\"start_time\"].map(lambda x: x.hour)\n", " return bike_trips" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " trip_id start_time end_time bikeid \\\n", "0 18709068 2018-05-31 23:59:00 2018-06-01 00:05:00 2233 \n", "1 18709067 2018-05-31 23:59:00 2018-06-01 00:07:00 3904 \n", "2 18709066 2018-05-31 23:58:00 2018-06-01 00:08:00 1969 \n", "3 18709065 2018-05-31 23:58:00 2018-06-01 00:21:00 457 \n", "4 18709064 2018-05-31 23:57:00 2018-06-01 00:08:00 4129 \n", "5 18709063 2018-05-31 23:57:00 2018-06-01 00:08:00 4156 \n", "6 18709062 2018-05-31 23:57:00 2018-06-01 00:06:00 2260 \n", "7 18709061 2018-05-31 23:57:00 2018-06-01 00:17:00 6112 \n", "8 18709060 2018-05-31 23:56:00 2018-06-01 00:02:00 2604 \n", "9 18709059 2018-05-31 23:56:00 2018-06-01 00:08:00 4310 \n", "10 18709058 2018-05-31 23:56:00 2018-06-01 00:15:00 99 \n", "11 18709057 2018-05-31 23:56:00 2018-06-01 00:06:00 2411 \n", "12 18709056 2018-05-31 23:56:00 2018-06-01 00:09:00 681 \n", "13 18709055 2018-05-31 23:56:00 2018-06-01 00:11:00 4656 \n", "14 18709054 2018-05-31 23:56:00 2018-06-01 00:17:00 5441 \n", "15 18709053 2018-05-31 23:55:00 2018-06-01 00:03:00 5377 \n", "16 18709052 2018-05-31 23:55:00 2018-06-01 00:02:00 2517 \n", "17 18709051 2018-05-31 23:55:00 2018-06-01 00:32:00 3206 \n", "18 18709050 2018-05-31 23:54:00 2018-06-01 00:13:00 4531 \n", "19 18709049 2018-05-31 23:54:00 2018-06-01 00:01:00 5967 \n", "20 18709048 2018-05-31 23:54:00 2018-06-01 00:05:00 3582 \n", "21 18709047 2018-05-31 23:54:00 2018-06-01 00:12:00 2388 \n", "22 18709046 2018-05-31 23:52:00 2018-06-01 00:41:00 5792 \n", "23 18709045 2018-05-31 23:52:00 2018-06-01 00:09:00 1859 \n", "24 18709044 2018-05-31 23:52:00 2018-06-01 00:09:00 163 \n", "25 18709043 2018-05-31 23:52:00 2018-06-01 00:03:00 4870 \n", "26 18709042 2018-05-31 23:51:00 2018-06-01 00:01:00 5260 \n", "27 18709041 2018-05-31 23:50:00 2018-06-01 00:15:00 4539 \n", "28 18709040 2018-05-31 23:49:00 2018-05-31 23:54:00 5293 \n", "29 18709039 2018-05-31 23:50:00 2018-05-31 23:52:00 4137 \n", "... ... ... ... ... \n", "600983 18000556 2018-04-01 01:00:00 2018-04-01 01:02:00 5952 \n", "600984 18000555 2018-04-01 00:58:00 2018-04-01 01:04:00 4303 \n", "600985 18000554 2018-04-01 00:58:00 2018-04-01 01:09:00 5195 \n", "600986 18000553 2018-04-01 00:54:00 2018-04-01 01:01:00 1695 \n", "600987 18000552 2018-04-01 00:50:00 2018-04-01 01:02:00 4659 \n", "600988 18000551 2018-04-01 00:45:00 2018-04-01 01:07:00 5482 \n", "600989 18000550 2018-04-01 00:40:00 2018-04-01 00:46:00 514 \n", "600990 18000549 2018-04-01 00:40:00 2018-04-01 00:45:00 4347 \n", "600991 18000548 2018-04-01 00:38:00 2018-04-01 00:50:00 2579 \n", "600992 18000547 2018-04-01 00:38:00 2018-04-01 00:44:00 5539 \n", "600993 18000546 2018-04-01 00:36:00 2018-04-01 00:39:00 3849 \n", "600994 18000545 2018-04-01 00:30:00 2018-04-01 00:32:00 3923 \n", "600995 18000544 2018-04-01 00:27:00 2018-04-01 01:00:00 364 \n", "600996 18000543 2018-04-01 00:26:00 2018-04-01 00:53:00 4691 \n", "600997 18000542 2018-04-01 00:26:00 2018-04-01 00:31:00 4222 \n", "600998 18000541 2018-04-01 00:26:00 2018-04-01 00:31:00 6333 \n", "600999 18000540 2018-04-01 00:24:00 2018-04-01 00:44:00 6401 \n", "601000 18000539 2018-04-01 00:23:00 2018-04-01 00:35:00 2028 \n", "601001 18000538 2018-04-01 00:23:00 2018-04-01 00:35:00 4213 \n", "601002 18000537 2018-04-01 00:20:00 2018-04-01 00:26:00 3869 \n", "601003 18000536 2018-04-01 00:20:00 2018-04-01 00:26:00 2602 \n", "601004 18000535 2018-04-01 00:18:00 2018-04-01 00:23:00 1977 \n", "601005 18000534 2018-04-01 00:17:00 2018-04-01 00:22:00 1323 \n", "601006 18000533 2018-04-01 00:15:00 2018-04-01 00:19:00 4570 \n", "601007 18000532 2018-04-01 00:11:00 2018-04-01 00:22:00 5962 \n", "601008 18000531 2018-04-01 00:10:00 2018-04-01 00:22:00 5065 \n", "601009 18000530 2018-04-01 00:07:00 2018-04-01 00:14:00 3851 \n", "601010 18000529 2018-04-01 00:07:00 2018-04-01 00:23:00 5165 \n", "601011 18000528 2018-04-01 00:06:00 2018-04-01 00:27:00 5000 \n", "601012 18000527 2018-04-01 00:04:00 2018-04-01 00:13:00 3819 \n", "\n", " tripduration from_station_id from_station_name \\\n", "0 367 21 Aberdeen St & Jackson Blvd \n", "1 521 172 Rush St & Cedar St \n", "2 563 106 State St & Pearson St \n", "3 1410 177 Theater on the Lake \n", "4 634 303 Broadway & Cornelia Ave \n", "5 644 303 Broadway & Cornelia Ave \n", "6 539 124 Damen Ave & Cullerton St \n", "7 1237 623 Michigan Ave & 8th St \n", "8 343 478 Rockwell St & Eastwood Ave \n", "9 686 337 Clark St & Chicago Ave \n", "10 1109 203 Western Ave & 21st St \n", "11 596 182 Wells St & Elm St \n", "12 793 50 Clark St & Congress Pkwy \n", "13 889 106 State St & Pearson St \n", "14 1265 498 California Ave & Fletcher St \n", "15 471 130 Damen Ave & Division St \n", "16 413 223 Clifton Ave & Armitage Ave \n", "17 2246 337 Clark St & Chicago Ave \n", "18 1121 210 Ashland Ave & Division St \n", "19 432 374 Western Ave & Walton St \n", "20 711 346 Ada St & Washington Blvd \n", "21 1082 349 Halsted St & Wrightwood Ave \n", "22 2901 181 LaSalle St & Illinois St \n", "23 986 128 Damen Ave & Chicago Ave \n", "24 977 128 Damen Ave & Chicago Ave \n", "25 687 176 Clark St & Elm St \n", "26 600 133 Kingsbury St & Kinzie St \n", "27 1443 128 Damen Ave & Chicago Ave \n", "28 266 249 Montrose Harbor \n", "29 139 605 University Library (NU) \n", "... ... ... ... \n", "600983 126 210 Ashland Ave & Division St \n", "600984 351 284 Michigan Ave & Jackson Blvd \n", "600985 653 305 Western Ave & Division St \n", "600986 401 426 Ellis Ave & 60th St \n", "600987 730 47 State St & Kinzie St \n", "600988 1329 517 Clark St & Jarvis Ave \n", "600989 348 117 Wilton Ave & Belmont Ave \n", "600990 299 506 Spaulding Ave & Armitage Ave \n", "600991 732 86 Eckhart Park \n", "600992 362 176 Clark St & Elm St \n", "600993 183 152 Lincoln Ave & Diversey Pkwy \n", "600994 158 81 Daley Center Plaza \n", "600995 1958 15 Racine Ave & 18th St \n", "600996 1635 161 Rush St & Superior St \n", "600997 311 74 Kingsbury St & Erie St \n", "600998 301 145 Mies van der Rohe Way & Chestnut St \n", "600999 1177 596 Benson Ave & Church St \n", "601000 741 31 Franklin St & Chicago Ave \n", "601001 702 31 Franklin St & Chicago Ave \n", "601002 367 121 Blackstone Ave & Hyde Park Blvd \n", "601003 382 121 Blackstone Ave & Hyde Park Blvd \n", "601004 282 130 Damen Ave & Division St \n", "601005 353 130 Damen Ave & Division St \n", "601006 238 128 Damen Ave & Chicago Ave \n", "601007 659 244 Ravenswood Ave & Irving Park Rd \n", "601008 709 228 Damen Ave & Melrose Ave \n", "601009 434 241 Morgan St & Polk St \n", "601010 960 106 State St & Pearson St \n", "601011 1225 157 Lake Shore Dr & Wellington Ave \n", "601012 499 22 May St & Taylor St \n", "\n", " to_station_id to_station_name usertype gender \\\n", "0 198 Green St & Madison St Subscriber Male \n", "1 26 McClurg Ct & Illinois St Subscriber Female \n", "2 47 State St & Kinzie St Subscriber Female \n", "3 343 Racine Ave & Wrightwood Ave Subscriber Female \n", "4 230 Lincoln Ave & Roscoe St Subscriber Male \n", "5 230 Lincoln Ave & Roscoe St Subscriber Female \n", "6 15 Racine Ave & 18th St Subscriber Male \n", "7 59 Wabash Ave & Roosevelt Rd Subscriber Male \n", "8 242 Damen Ave & Leland Ave Customer NaN \n", "9 94 Clark St & Armitage Ave Subscriber Female \n", "10 443 Millard Ave & 26th St Subscriber Male \n", "11 56 Desplaines St & Kinzie St Subscriber Male \n", "12 273 Michigan Ave & 18th St Subscriber Male \n", "13 210 Ashland Ave & Division St Subscriber Female \n", "14 299 Halsted St & Roscoe St Subscriber Male \n", "15 16 Paulina Ave & North Ave Subscriber Male \n", "16 94 Clark St & Armitage Ave Subscriber Male \n", "17 225 Halsted St & Dickens Ave Subscriber Female \n", "18 250 Ashland Ave & Wellington Ave Subscriber Male \n", "19 17 Honore St & Division St Subscriber Male \n", "20 128 Damen Ave & Chicago Ave Customer NaN \n", "21 128 Damen Ave & Chicago Ave Customer NaN \n", "22 199 Wabash Ave & Grand Ave Customer NaN \n", "23 320 Loomis St & Lexington St Customer NaN \n", "24 320 Loomis St & Lexington St Customer NaN \n", "25 133 Kingsbury St & Kinzie St Subscriber Male \n", "26 71 Morgan St & Lake St Subscriber Male \n", "27 80 Aberdeen St & Monroe St Subscriber Male \n", "28 245 Clarendon Ave & Junior Ter Subscriber Male \n", "29 603 Chicago Ave & Sheridan Rd Subscriber Male \n", "... ... ... ... ... \n", "600983 29 Noble St & Milwaukee Ave Subscriber Male \n", "600984 255 Indiana Ave & Roosevelt Rd Subscriber Female \n", "600985 222 Milwaukee Ave & Rockwell St Subscriber Male \n", "600986 425 Harper Ave & 59th St Subscriber Male \n", "600987 623 Michigan Ave & 8th St Customer NaN \n", "600988 344 Ravenswood Ave & Lawrence Ave Subscriber Male \n", "600989 131 Lincoln Ave & Belmont Ave Subscriber Female \n", "600990 503 Drake Ave & Fullerton Ave Subscriber Male \n", "600991 176 Clark St & Elm St Subscriber Male \n", "600992 337 Clark St & Chicago Ave Customer NaN \n", "600993 343 Racine Ave & Wrightwood Ave Subscriber Male \n", "600994 43 Michigan Ave & Washington St Subscriber Male \n", "600995 196 Cityfront Plaza Dr & Pioneer Ct Customer NaN \n", "600996 123 California Ave & Milwaukee Ave Subscriber Male \n", "600997 56 Desplaines St & Kinzie St Subscriber Male \n", "600998 24 Fairbanks Ct & Grand Ave Subscriber Male \n", "600999 517 Clark St & Jarvis Ave Subscriber Male \n", "601000 180 Ritchie Ct & Banks St Subscriber Male \n", "601001 180 Ritchie Ct & Banks St Subscriber Male \n", "601002 351 Cottage Grove Ave & 51st St Subscriber Female \n", "601003 351 Cottage Grove Ave & 51st St Subscriber Female \n", "601004 69 Damen Ave & Pierce Ave Subscriber Male \n", "601005 69 Damen Ave & Pierce Ave Subscriber Male \n", "601006 130 Damen Ave & Division St Subscriber Male \n", "601007 325 Clark St & Winnemac Ave Subscriber Male \n", "601008 219 Damen Ave & Cortland St Subscriber Male \n", "601009 171 May St & Cullerton St Subscriber Male \n", "601010 106 State St & Pearson St Customer NaN \n", "601011 190 Southport Ave & Wrightwood Ave Subscriber Male \n", "601012 171 May St & Cullerton St Subscriber Male \n", "\n", " birthyear hour \n", "0 1992.0 23 \n", "1 1985.0 23 \n", "2 1986.0 23 \n", "3 1996.0 23 \n", "4 1979.0 23 \n", "5 1983.0 23 \n", "6 1974.0 23 \n", "7 1985.0 23 \n", "8 NaN 23 \n", "9 1993.0 23 \n", "10 1982.0 23 \n", "11 1974.0 23 \n", "12 1984.0 23 \n", "13 1994.0 23 \n", "14 1994.0 23 \n", "15 1988.0 23 \n", "16 1991.0 23 \n", "17 1989.0 23 \n", "18 1983.0 23 \n", "19 1971.0 23 \n", "20 NaN 23 \n", "21 NaN 23 \n", "22 NaN 23 \n", "23 NaN 23 \n", "24 NaN 23 \n", "25 1979.0 23 \n", "26 1989.0 23 \n", "27 1997.0 23 \n", "28 1961.0 23 \n", "29 1997.0 23 \n", "... ... ... \n", "600983 1992.0 1 \n", "600984 1979.0 0 \n", "600985 1983.0 0 \n", "600986 1965.0 0 \n", "600987 NaN 0 \n", "600988 1974.0 0 \n", "600989 1993.0 0 \n", "600990 1992.0 0 \n", "600991 1993.0 0 \n", "600992 NaN 0 \n", "600993 1989.0 0 \n", "600994 1993.0 0 \n", "600995 NaN 0 \n", "600996 1976.0 0 \n", "600997 1985.0 0 \n", "600998 1984.0 0 \n", "600999 1974.0 0 \n", "601000 1986.0 0 \n", "601001 1985.0 0 \n", "601002 1992.0 0 \n", "601003 1992.0 0 \n", "601004 1990.0 0 \n", "601005 1991.0 0 \n", "601006 1978.0 0 \n", "601007 1991.0 0 \n", "601008 1983.0 0 \n", "601009 1998.0 0 \n", "601010 NaN 0 \n", "601011 1965.0 0 \n", "601012 1994.0 0 \n", "\n", "[601013 rows x 13 columns]\n" ] } ], "source": [ "trips = get_and_format_trip_data('Divvy_Trips_2018_0405.csv')\n", "print(trips.head(10))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " trip_id start_time end_time bikeid tripduration \\\n", "0 18709068 2018-05-31 23:59:00 2018-06-01 00:05:00 2233 367 \n", "1 18709067 2018-05-31 23:59:00 2018-06-01 00:07:00 3904 521 \n", "2 18709066 2018-05-31 23:58:00 2018-06-01 00:08:00 1969 563 \n", "3 18709065 2018-05-31 23:58:00 2018-06-01 00:21:00 457 1410 \n", "4 18709064 2018-05-31 23:57:00 2018-06-01 00:08:00 4129 634 \n", "5 18709063 2018-05-31 23:57:00 2018-06-01 00:08:00 4156 644 \n", "6 18709062 2018-05-31 23:57:00 2018-06-01 00:06:00 2260 539 \n", "7 18709061 2018-05-31 23:57:00 2018-06-01 00:17:00 6112 1237 \n", "8 18709060 2018-05-31 23:56:00 2018-06-01 00:02:00 2604 343 \n", "9 18709059 2018-05-31 23:56:00 2018-06-01 00:08:00 4310 686 \n", "\n", " from_station_id from_station_name to_station_id \\\n", "0 21 Aberdeen St & Jackson Blvd 198 \n", "1 172 Rush St & Cedar St 26 \n", "2 106 State St & Pearson St 47 \n", "3 177 Theater on the Lake 343 \n", "4 303 Broadway & Cornelia Ave 230 \n", "5 303 Broadway & Cornelia Ave 230 \n", "6 124 Damen Ave & Cullerton St 15 \n", "7 623 Michigan Ave & 8th St 59 \n", "8 478 Rockwell St & Eastwood Ave 242 \n", "9 337 Clark St & Chicago Ave 94 \n", "\n", " to_station_name usertype gender birthyear hour \n", "0 Green St & Madison St Subscriber Male 1992.0 23 \n", "1 McClurg Ct & Illinois St Subscriber Female 1985.0 23 \n", "2 State St & Kinzie St Subscriber Female 1986.0 23 \n", "3 Racine Ave & Wrightwood Ave Subscriber Female 1996.0 23 \n", "4 Lincoln Ave & Roscoe St Subscriber Male 1979.0 23 \n", "5 Lincoln Ave & Roscoe St Subscriber Female 1983.0 23 \n", "6 Racine Ave & 18th St Subscriber Male 1974.0 23 \n", "7 Wabash Ave & Roosevelt Rd Subscriber Male 1985.0 23 \n", "8 Damen Ave & Leland Ave Customer NaN NaN 23 \n", "9 Clark St & Armitage Ave Subscriber Female 1993.0 23 \n" ] } ], "source": [ "print(trips.head(10))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "def get_station_list():\n", " '''output: pandas data frame'''\n", " station_api_key = 'stationBeanList'\n", " try:\n", " response = requests.get('https://feeds.divvybikes.com/stations/stations.json')\n", " station_data = response.json()\n", " station_list = station_data[station_api_key]\n", " stations = pd.DataFrame.from_records(station_list, index='id')\n", " except:\n", " print('something went wrong')\n", " return stations" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "stations = get_station_list()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " altitude availableBikes availableDocks city is_renting kioskType \\\n", "id \n", "2 7 32 Chicago True K3_FULL \n", "3 6 47 Chicago True K3_FULL \n", "4 11 12 Chicago True K3_FULL \n", "5 6 17 Chicago True K3_FULL \n", "6 10 23 Chicago True K3_FULL \n", "7 2 15 Chicago True K3_FULL \n", "9 7 7 Chicago True K3_FULL \n", "11 6 4 Chicago True K3_FULL \n", "12 3 12 Chicago True K3_FULL \n", "13 22 13 Chicago True K3_FULL \n", "\n", " landMark lastCommunicationTime latitude location longitude \\\n", "id \n", "2 15541 2018-09-17 16:50:03 41.876470 -87.620340 \n", "3 15544 2018-09-17 16:51:42 41.867226 -87.615355 \n", "4 15545 2018-09-17 16:50:40 41.856268 -87.613348 \n", "5 13030 2018-09-17 16:48:19 41.874053 620 S. State St. -87.627716 \n", "6 15548 2018-09-17 16:50:16 41.886976 -87.612813 \n", "7 15534 2018-09-17 16:51:04 41.886349 -87.617517 \n", "9 15695 2018-09-17 16:49:59 41.828792 NA1 -87.680604 \n", "11 15496 2018-09-17 16:50:21 41.766638 -87.576450 \n", "12 15497 2018-09-17 16:49:04 41.766409 MLSW -87.565688 \n", "13 13066 2018-09-17 16:49:10 41.932418 2790 N.Wilton Ave -87.652705 \n", "\n", " postalCode stAddress1 stAddress2 \\\n", "id \n", "2 60605 Buckingham Fountain \n", "3 Shedd Aquarium \n", "4 Burnham Harbor \n", "5 60605 State St & Harrison St \n", "6 60601 Dusable Harbor \n", "7 60601 Field Blvd & South Water St \n", "9 60609 Leavitt St & Archer Ave \n", "11 60649 Jeffery Blvd & 71st St \n", "12 60649 South Shore Dr & 71st St \n", "13 60614 Wilton Ave & Diversey Pkwy \n", "\n", " stationName status statusKey statusValue \\\n", "id \n", "2 Buckingham Fountain IN_SERVICE 1 In Service \n", "3 Shedd Aquarium IN_SERVICE 1 In Service \n", "4 Burnham Harbor IN_SERVICE 1 In Service \n", "5 State St & Harrison St IN_SERVICE 1 In Service \n", "6 Dusable Harbor IN_SERVICE 1 In Service \n", "7 Field Blvd & South Water St IN_SERVICE 1 In Service \n", "9 Leavitt St & Archer Ave IN_SERVICE 1 In Service \n", "11 Jeffery Blvd & 71st St IN_SERVICE 1 In Service \n", "12 South Shore Dr & 71st St IN_SERVICE 1 In Service \n", "13 Wilton Ave & Diversey Pkwy IN_SERVICE 1 In Service \n", "\n", " testStation totalDocks \n", "id \n", "2 False 39 \n", "3 False 55 \n", "4 False 23 \n", "5 False 23 \n", "6 False 37 \n", "7 False 19 \n", "9 False 15 \n", "11 False 11 \n", "12 False 15 \n", "13 False 35 \n" ] } ], "source": [ "print(stations.head(10))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def add_trip_counts_to_stations(stations, trips):\n", " '''\n", " input: data frames\n", " output: data frame\n", " '''\n", " departure_counts = trips.groupby('from_station_id').count()\n", " # just for fun, to see the type of data \n", " print('trip type', type(trips.groupby('from_station_id')))\n", " # just for fun, to see how it looks \n", " print('what departure counts looks like after gouping and counting', departure_counts.head(10))\n", " departure_counts = departure_counts.iloc[:, [0]]\n", " print('type of departure counts after iloc', type(departure_counts))\n", " print('departure counts after iloc', departure_counts.head(10))\n", " departure_counts.columns = ['Departure Count']\n", " print('departure counts after renaming the colun', departure_counts.head(10))\n", "\n", " arrival_counts = trips.groupby('to_station_id').count()\n", " arrival_counts = arrival_counts.iloc[:, [0]]\n", " arrival_counts.columns = ['Arrival Count']\n", "\n", " stations = pd.merge(departure_counts, stations,\n", " right_on='id',\n", " left_index=True).merge(arrival_counts,\n", " left_on='id',\n", " right_index=True)\n", " return stations" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "trip type \n", "what departure counts looks like after gouping and counting trip_id start_time end_time bikeid tripduration \\\n", "from_station_id \n", "2 1914 1914 1914 1914 1914 \n", "3 4208 4208 4208 4208 4208 \n", "4 2203 2203 2203 2203 2203 \n", "5 1345 1345 1345 1345 1345 \n", "6 2628 2628 2628 2628 2628 \n", "7 1910 1910 1910 1910 1910 \n", "9 100 100 100 100 100 \n", "11 52 52 52 52 52 \n", "12 80 80 80 80 80 \n", "13 2072 2072 2072 2072 2072 \n", "\n", " from_station_name to_station_id to_station_name usertype \\\n", "from_station_id \n", "2 1914 1914 1914 1914 \n", "3 4208 4208 4208 4208 \n", "4 2203 2203 2203 2203 \n", "5 1345 1345 1345 1345 \n", "6 2628 2628 2628 2628 \n", "7 1910 1910 1910 1910 \n", "9 100 100 100 100 \n", "11 52 52 52 52 \n", "12 80 80 80 80 \n", "13 2072 2072 2072 2072 \n", "\n", " gender birthyear hour \n", "from_station_id \n", "2 430 433 1914 \n", "3 981 983 4208 \n", "4 1598 1599 2203 \n", "5 1124 1125 1345 \n", "6 949 950 2628 \n", "7 1501 1501 1910 \n", "9 95 95 100 \n", "11 52 52 52 \n", "12 61 61 80 \n", "13 1920 1920 2072 \n", "type of departure counts after iloc \n", "departure counts after iloc trip_id\n", "from_station_id \n", "2 1914\n", "3 4208\n", "4 2203\n", "5 1345\n", "6 2628\n", "7 1910\n", "9 100\n", "11 52\n", "12 80\n", "13 2072\n", "departure counts after renaming the colun Departure Count\n", "from_station_id \n", "2 1914\n", "3 4208\n", "4 2203\n", "5 1345\n", "6 2628\n", "7 1910\n", "9 100\n", "11 52\n", "12 80\n", "13 2072\n" ] } ], "source": [ "stations = add_trip_counts_to_stations(stations, trips)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Departure Count altitude availableBikes availableDocks city \\\n", "id \n", "2 1914 7 32 Chicago \n", "3 4208 6 47 Chicago \n", "4 2203 11 12 Chicago \n", "5 1345 6 17 Chicago \n", "6 2628 10 23 Chicago \n", "7 1910 2 15 Chicago \n", "9 100 7 7 Chicago \n", "11 52 6 4 Chicago \n", "12 80 3 12 Chicago \n", "13 2072 22 13 Chicago \n", "\n", " is_renting kioskType landMark lastCommunicationTime latitude \\\n", "id \n", "2 True K3_FULL 15541 2018-09-17 16:50:03 41.876470 \n", "3 True K3_FULL 15544 2018-09-17 16:51:42 41.867226 \n", "4 True K3_FULL 15545 2018-09-17 16:50:40 41.856268 \n", "5 True K3_FULL 13030 2018-09-17 16:48:19 41.874053 \n", "6 True K3_FULL 15548 2018-09-17 16:50:16 41.886976 \n", "7 True K3_FULL 15534 2018-09-17 16:51:04 41.886349 \n", "9 True K3_FULL 15695 2018-09-17 16:49:59 41.828792 \n", "11 True K3_FULL 15496 2018-09-17 16:50:21 41.766638 \n", "12 True K3_FULL 15497 2018-09-17 16:49:04 41.766409 \n", "13 True K3_FULL 13066 2018-09-17 16:49:10 41.932418 \n", "\n", " ... postalCode stAddress1 stAddress2 \\\n", "id ... \n", "2 ... 60605 Buckingham Fountain \n", "3 ... Shedd Aquarium \n", "4 ... Burnham Harbor \n", "5 ... 60605 State St & Harrison St \n", "6 ... 60601 Dusable Harbor \n", "7 ... 60601 Field Blvd & South Water St \n", "9 ... 60609 Leavitt St & Archer Ave \n", "11 ... 60649 Jeffery Blvd & 71st St \n", "12 ... 60649 South Shore Dr & 71st St \n", "13 ... 60614 Wilton Ave & Diversey Pkwy \n", "\n", " stationName status statusKey statusValue \\\n", "id \n", "2 Buckingham Fountain IN_SERVICE 1 In Service \n", "3 Shedd Aquarium IN_SERVICE 1 In Service \n", "4 Burnham Harbor IN_SERVICE 1 In Service \n", "5 State St & Harrison St IN_SERVICE 1 In Service \n", "6 Dusable Harbor IN_SERVICE 1 In Service \n", "7 Field Blvd & South Water St IN_SERVICE 1 In Service \n", "9 Leavitt St & Archer Ave IN_SERVICE 1 In Service \n", "11 Jeffery Blvd & 71st St IN_SERVICE 1 In Service \n", "12 South Shore Dr & 71st St IN_SERVICE 1 In Service \n", "13 Wilton Ave & Diversey Pkwy IN_SERVICE 1 In Service \n", "\n", " testStation totalDocks Arrival Count \n", "id \n", "2 False 39 1729 \n", "3 False 55 3439 \n", "4 False 23 2190 \n", "5 False 23 1530 \n", "6 False 37 2092 \n", "7 False 19 1849 \n", "9 False 15 95 \n", "11 False 11 43 \n", "12 False 15 104 \n", "13 False 35 2261 \n", "\n", "[10 rows x 22 columns]\n" ] } ], "source": [ "print(stations.head(10))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "def put_stations_on_map(stations):\n", " '''\n", " input: data frame\n", " output: folium map\n", " '''\n", " map = folium.Map(location=[41.88, -87.62],\n", " zoom_start=13,\n", " tiles=\"CartoDB dark_matter\")\n", " for index, station in stations.iterrows():\n", " popup_text = \"{}
Total departures: {}
Total arrivals: {}
\"\n", " popup_text = popup_text.format(stations.at[index, \"stationName\"],\n", " stations.at[index, \"Arrival Count\"],\n", " stations.at[index, \"Departure Count\"],\n", " )\n", " folium.CircleMarker(location=[stations.at[index, 'latitude'],\n", " stations.at[index, 'longitude']],\n", " fill=True,\n", " popup=popup_text).add_to(map)\n", " return map" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "folium_map = put_stations_on_map(stations)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folium_map" ] }, { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }