diff --git a/starter_kit/monica_exercises/basics_04_steps.ipynb b/starter_kit/monica_exercises/basics_04_steps.ipynb index c491493a0..74d4c886d 100644 --- a/starter_kit/monica_exercises/basics_04_steps.ipynb +++ b/starter_kit/monica_exercises/basics_04_steps.ipynb @@ -24,10 +24,27 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 1, "id": "9305f4e1-446d-432f-bc0e-e69284fdce71", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/opt/conda/lib/python3.9/site-packages/geopandas/_compat.py:124: UserWarning: The Shapely GEOS version (3.11.1-CAPI-1.17.1) is incompatible with the GEOS version PyGEOS was compiled with (3.10.1-CAPI-1.16.0). Conversions between both will be slow.\n", + " warnings.warn(\n", + "/tmp/ipykernel_1381/1652705367.py:1: DeprecationWarning: Shapely 2.0 is installed, but because PyGEOS is also installed, GeoPandas still uses PyGEOS by default. However, starting with version 0.14, the default will switch to Shapely. To force to use Shapely 2.0 now, you can either uninstall PyGEOS or set the environment variable USE_PYGEOS=0. You can do this before starting the Python process, or in your code before importing geopandas:\n", + "\n", + "import os\n", + "os.environ['USE_PYGEOS'] = '0'\n", + "import geopandas\n", + "\n", + "In the next release, GeoPandas will switch to using Shapely by default, even if PyGEOS is installed. If you only have PyGEOS installed to get speed-ups, this switch should be smooth. However, if you are using PyGEOS directly (calling PyGEOS functions on geometries from GeoPandas), this will then stop working and you are encouraged to migrate from PyGEOS to Shapely 2.0 (https://shapely.readthedocs.io/en/latest/migration_pygeos.html).\n", + " import geopandas as gpd\n" + ] + } + ], "source": [ "import geopandas as gpd\n", "import pandas as pd\n", @@ -44,7 +61,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 2, "id": "0e4cc814-de2b-4484-a702-b20166587024", "metadata": {}, "outputs": [ @@ -137,7 +154,7 @@ "4 Juniper Ave @ Valley St (eastbound) " ] }, - "execution_count": 7, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -166,11 +183,12 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 3, "id": "a83cda9e-c642-4acf-8668-d5626dd7d948", "metadata": {}, "outputs": [], "source": [ + "#Listing the feed_key for Caltrain and Merced\n", "FEEDS = [\n", " \"25c6505166c01099b2f6f2de173e20b9\", # Caltrain\n", " \"52639f09eb535f75b33d2c6a654cb89e\", # Merced\n", @@ -189,7 +207,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 4, "id": "4fd01576-1163-421b-b7d1-02e6d5c065e2", "metadata": {}, "outputs": [ @@ -262,8 +280,57 @@ " -122.411709\n", " San Bruno Caltrain Station\n", " \n", + " \n", + " ...\n", + " ...\n", + " ...\n", + " ...\n", + " ...\n", + " ...\n", + " \n", + " \n", + " 219\n", + " 52639f09eb535f75b33d2c6a654cb89e\n", + " 782486\n", + " 36.981746\n", + " -120.627963\n", + " Golden Gate Ave & Valeria St Stop\n", + " \n", + " \n", + " 19\n", + " 52639f09eb535f75b33d2c6a654cb89e\n", + " 782487\n", + " 36.982797\n", + " -120.625370\n", + " Lucerne Ave and Ida St\n", + " \n", + " \n", + " 85\n", + " 52639f09eb535f75b33d2c6a654cb89e\n", + " 782489\n", + " 36.992336\n", + " -120.626187\n", + " Obanion Park\n", + " \n", + " \n", + " 22\n", + " 52639f09eb535f75b33d2c6a654cb89e\n", + " 844203\n", + " 37.391296\n", + " -120.722486\n", + " Foster Farms (To Livingston)\n", + " \n", + " \n", + " 99\n", + " 52639f09eb535f75b33d2c6a654cb89e\n", + " 844204\n", + " 37.058013\n", + " -120.822685\n", + " Food 4 Less / Dollar Tree\n", + " \n", " \n", "\n", + "

232 rows × 5 columns

\n", "" ], "text/plain": [ @@ -273,22 +340,150 @@ "58 25c6505166c01099b2f6f2de173e20b9 70031 37.709537 -122.401586 \n", "1 25c6505166c01099b2f6f2de173e20b9 70042 37.655942 -122.404980 \n", "52 25c6505166c01099b2f6f2de173e20b9 70052 37.630461 -122.411709 \n", + ".. ... ... ... ... \n", + "219 52639f09eb535f75b33d2c6a654cb89e 782486 36.981746 -120.627963 \n", + "19 52639f09eb535f75b33d2c6a654cb89e 782487 36.982797 -120.625370 \n", + "85 52639f09eb535f75b33d2c6a654cb89e 782489 36.992336 -120.626187 \n", + "22 52639f09eb535f75b33d2c6a654cb89e 844203 37.391296 -120.722486 \n", + "99 52639f09eb535f75b33d2c6a654cb89e 844204 37.058013 -120.822685 \n", "\n", " stop_name \n", "134 22nd Street \n", "50 22nd Street Caltrain Station \n", "58 Bayshore Caltrain Station \n", "1 South San Francisco Caltrain Station \n", - "52 San Bruno Caltrain Station " + "52 San Bruno Caltrain Station \n", + ".. ... \n", + "219 Golden Gate Ave & Valeria St Stop \n", + "19 Lucerne Ave and Ida St \n", + "85 Obanion Park \n", + "22 Foster Farms (To Livingston) \n", + "99 Food 4 Less / Dollar Tree \n", + "\n", + "[232 rows x 5 columns]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stops" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9e0d2595-f9b8-4c23-831d-0ff6d8feb16d", + "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", + "
feed_keystop_idstop_latstop_lonstop_namegeometry
13425c6505166c01099b2f6f2de173e20b922nd_street37.756972-122.39249222nd StreetPOINT (-122.39249 37.75697)
5025c6505166c01099b2f6f2de173e20b97002137.757599-122.39188022nd Street Caltrain StationPOINT (-122.39188 37.75760)
5825c6505166c01099b2f6f2de173e20b97003137.709537-122.401586Bayshore Caltrain StationPOINT (-122.40159 37.70954)
125c6505166c01099b2f6f2de173e20b97004237.655942-122.404980South San Francisco Caltrain StationPOINT (-122.40498 37.65594)
5225c6505166c01099b2f6f2de173e20b97005237.630461-122.411709San Bruno Caltrain StationPOINT (-122.41171 37.63046)
\n", + "
" + ], + "text/plain": [ + " feed_key stop_id stop_lat stop_lon \\\n", + "134 25c6505166c01099b2f6f2de173e20b9 22nd_street 37.756972 -122.392492 \n", + "50 25c6505166c01099b2f6f2de173e20b9 70021 37.757599 -122.391880 \n", + "58 25c6505166c01099b2f6f2de173e20b9 70031 37.709537 -122.401586 \n", + "1 25c6505166c01099b2f6f2de173e20b9 70042 37.655942 -122.404980 \n", + "52 25c6505166c01099b2f6f2de173e20b9 70052 37.630461 -122.411709 \n", + "\n", + " stop_name geometry \n", + "134 22nd Street POINT (-122.39249 37.75697) \n", + "50 22nd Street Caltrain Station POINT (-122.39188 37.75760) \n", + "58 Bayshore Caltrain Station POINT (-122.40159 37.70954) \n", + "1 South San Francisco Caltrain Station POINT (-122.40498 37.65594) \n", + "52 San Bruno Caltrain Station POINT (-122.41171 37.63046) " ] }, - "execution_count": 9, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "stops.head()" + "#Turn the point data into geodataframe with geopandas\n", + "stop_geometry= gpd.GeoDataFrame(\n", + " stops, geometry=gpd.points_from_xy(stops.stop_lon, stops.stop_lat, crs=\"EPSG:4326\"))\n", + "\n", + "stop_geometry.head()" ] }, { @@ -309,11 +504,213 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "7b83b31c-3fde-4933-a3d1-f4e97be2cd4f", "metadata": {}, - "outputs": [], - "source": [] + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
feed_keystop_idstop_latstop_lonstop_nameoperatoragency
13425c6505166c01099b2f6f2de173e20b922nd_street37.756972-122.39249222nd StreetCaltrainCaltrain
5025c6505166c01099b2f6f2de173e20b97002137.757599-122.39188022nd Street Caltrain StationCaltrainCaltrain
5825c6505166c01099b2f6f2de173e20b97003137.709537-122.401586Bayshore Caltrain StationCaltrainCaltrain
125c6505166c01099b2f6f2de173e20b97004237.655942-122.404980South San Francisco Caltrain StationCaltrainCaltrain
5225c6505166c01099b2f6f2de173e20b97005237.630461-122.411709San Bruno Caltrain StationCaltrainCaltrain
........................
21952639f09eb535f75b33d2c6a654cb89e78248636.981746-120.627963Golden Gate Ave & Valeria St StopMercedMerced
1952639f09eb535f75b33d2c6a654cb89e78248736.982797-120.625370Lucerne Ave and Ida StMercedMerced
8552639f09eb535f75b33d2c6a654cb89e78248936.992336-120.626187Obanion ParkMercedMerced
2252639f09eb535f75b33d2c6a654cb89e84420337.391296-120.722486Foster Farms (To Livingston)MercedMerced
9952639f09eb535f75b33d2c6a654cb89e84420437.058013-120.822685Food 4 Less / Dollar TreeMercedMerced
\n", + "

232 rows × 7 columns

\n", + "
" + ], + "text/plain": [ + " feed_key stop_id stop_lat stop_lon \\\n", + "134 25c6505166c01099b2f6f2de173e20b9 22nd_street 37.756972 -122.392492 \n", + "50 25c6505166c01099b2f6f2de173e20b9 70021 37.757599 -122.391880 \n", + "58 25c6505166c01099b2f6f2de173e20b9 70031 37.709537 -122.401586 \n", + "1 25c6505166c01099b2f6f2de173e20b9 70042 37.655942 -122.404980 \n", + "52 25c6505166c01099b2f6f2de173e20b9 70052 37.630461 -122.411709 \n", + ".. ... ... ... ... \n", + "219 52639f09eb535f75b33d2c6a654cb89e 782486 36.981746 -120.627963 \n", + "19 52639f09eb535f75b33d2c6a654cb89e 782487 36.982797 -120.625370 \n", + "85 52639f09eb535f75b33d2c6a654cb89e 782489 36.992336 -120.626187 \n", + "22 52639f09eb535f75b33d2c6a654cb89e 844203 37.391296 -120.722486 \n", + "99 52639f09eb535f75b33d2c6a654cb89e 844204 37.058013 -120.822685 \n", + "\n", + " stop_name operator agency \n", + "134 22nd Street Caltrain Caltrain \n", + "50 22nd Street Caltrain Station Caltrain Caltrain \n", + "58 Bayshore Caltrain Station Caltrain Caltrain \n", + "1 South San Francisco Caltrain Station Caltrain Caltrain \n", + "52 San Bruno Caltrain Station Caltrain Caltrain \n", + ".. ... ... ... \n", + "219 Golden Gate Ave & Valeria St Stop Merced Merced \n", + "19 Lucerne Ave and Ida St Merced Merced \n", + "85 Obanion Park Merced Merced \n", + "22 Foster Farms (To Livingston) Merced Merced \n", + "99 Food 4 Less / Dollar Tree Merced Merced \n", + "\n", + "[232 rows x 7 columns]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#making a new table as back up\n", + "stops_tbl = stops\n", + "\n", + "#Function to set operator to agency\n", + "def operator_function(row):\n", + " if row.feed_key == '52639f09eb535f75b33d2c6a654cb89e':\n", + " return 'Merced'\n", + " elif row.feed_key == '25c6505166c01099b2f6f2de173e20b9':\n", + " return 'Caltrain'\n", + " else:\n", + " return 'other'\n", + "#New column for operator\n", + "stops_tbl['operator'] = stops.apply(operator_function, axis = 1)\n", + "\n", + "#Using dictionary to set operator to agency\n", + "agency_dictionary={'52639f09eb535f75b33d2c6a654cb89e': 'Merced', '25c6505166c01099b2f6f2de173e20b9': 'Caltrain'}\n", + "#New column for agency\n", + "stops['agency'] = stops.feed_key.map(agency_dictionary)\n", + "\n", + "stops_tbl" + ] }, { "cell_type": "markdown",