{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import folium\n", "folium_map = folium.Map(location=[40.738, -73.98],\n", " zoom_start=13,\n", " tiles=\"CartoDB dark_matter\")\n", "\n", "marker = folium.CircleMarker(location=[40.738, -73.98])\n", "marker.add_to(folium_map)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folium_map" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "bike_data = pd.read_csv(\"201610-citibike-tripdata.csv\")\n", "\n", "bike_data[\"Start Time\"] = pd.to_datetime(bike_data[\"Start Time\"])\n", "bike_data[\"Stop Time\"] = pd.to_datetime(bike_data[\"Stop Time\"])\n", "bike_data[\"hour\"] = bike_data[\"Start Time\"].map(lambda x: x.hour)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Trip DurationStart TimeStop TimeStart Station IDStart Station NameStart Station LatitudeStart Station LongitudeEnd Station IDEnd Station NameEnd Station LatitudeEnd Station LongitudeBike IDUser TypeBirth YearGenderhour
03282016-10-01 00:00:072016-10-01 00:05:35471Grand St & Havemeyer St40.712868-73.9569813077Stagg St & Union Ave40.708771-73.95095325254Subscriber1992.010
13982016-10-01 00:00:112016-10-01 00:06:493147E 85 St & 3 Ave40.778012-73.95407131401 Ave & E 78 St40.771404-73.95351717810Subscriber1988.020
24302016-10-01 00:00:142016-10-01 00:07:25345W 13 St & 6 Ave40.736494-73.997044470W 20 St & 8 Ave40.743453-74.00004020940Subscriber1965.010
33512016-10-01 00:00:212016-10-01 00:06:123307West End Ave & W 94 St40.794165-73.9741243357W 106 St & Amsterdam Ave40.800836-73.96644919086Subscriber1993.010
426932016-10-01 00:00:212016-10-01 00:45:1534288 Ave & W 16 St40.740983-74.0017023323W 106 St & Central Park West40.798186-73.96059126502Subscriber1991.010
\n", "
" ], "text/plain": [ " Trip Duration Start Time Stop Time Start Station ID \\\n", "0 328 2016-10-01 00:00:07 2016-10-01 00:05:35 471 \n", "1 398 2016-10-01 00:00:11 2016-10-01 00:06:49 3147 \n", "2 430 2016-10-01 00:00:14 2016-10-01 00:07:25 345 \n", "3 351 2016-10-01 00:00:21 2016-10-01 00:06:12 3307 \n", "4 2693 2016-10-01 00:00:21 2016-10-01 00:45:15 3428 \n", "\n", " Start Station Name Start Station Latitude Start Station Longitude \\\n", "0 Grand St & Havemeyer St 40.712868 -73.956981 \n", "1 E 85 St & 3 Ave 40.778012 -73.954071 \n", "2 W 13 St & 6 Ave 40.736494 -73.997044 \n", "3 West End Ave & W 94 St 40.794165 -73.974124 \n", "4 8 Ave & W 16 St 40.740983 -74.001702 \n", "\n", " End Station ID End Station Name End Station Latitude \\\n", "0 3077 Stagg St & Union Ave 40.708771 \n", "1 3140 1 Ave & E 78 St 40.771404 \n", "2 470 W 20 St & 8 Ave 40.743453 \n", "3 3357 W 106 St & Amsterdam Ave 40.800836 \n", "4 3323 W 106 St & Central Park West 40.798186 \n", "\n", " End Station Longitude Bike ID User Type Birth Year Gender hour \n", "0 -73.950953 25254 Subscriber 1992.0 1 0 \n", "1 -73.953517 17810 Subscriber 1988.0 2 0 \n", "2 -74.000040 20940 Subscriber 1965.0 1 0 \n", "3 -73.966449 19086 Subscriber 1993.0 1 0 \n", "4 -73.960591 26502 Subscriber 1991.0 1 0 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bike_data.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# select the first occurrence of each station id\n", "locations = bike_data.groupby(\"Start Station ID\").first()\n", "\n", "# and select only the tree columns we are interested in\n", "locations = locations.loc[:, [\"Start Station Latitude\",\n", " \"Start Station Longitude\",\n", " \"Start Station Name\"]]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "subset = bike_data[bike_data[\"hour\"]==18]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "departure_counts = subset.groupby(\"Start Station ID\").count()\n", "\n", "# select one column\n", "departure_counts = departure_counts.iloc[:,[0]]\n", "\n", "# and rename that column\n", "departure_counts.columns= [\"Departure Count\"]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "arrival_counts = subset.groupby(\"End Station ID\").count()\n", "\n", "# select one column\n", "arrival_counts = arrival_counts.iloc[:,[0]]\n", "\n", "# and rename that column\n", "arrival_counts.columns= [\"Arrival Count\"]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "trip_counts = departure_counts.join(locations).join(arrival_counts)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "for index, row in trip_counts.iterrows():\n", " net_departures = (row[\"Departure Count\"]-row[\"Arrival Count\"])\n", " radius = net_departures/20\n", " if net_departures>0:\n", " color=\"#E37222\" # tangerine\n", " else:\n", " color=\"#0A8A9F\" # teal\n", " \n", " folium.CircleMarker(location=(row[\"Start Station Latitude\"],\n", " row[\"Start Station Longitude\"]),\n", " radius=radius,\n", " color=color,\n", " fill=True).add_to(folium_map)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 11, "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }