{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "4z9RusIWHOne" }, "source": [ "# Bienvenid@s!\n", "\n", "**Esta celda de texto es un Markdown. Es el tipo de celda donde podemos escribir texto que no es código.**\n", "\n", "Comencemos de inmediato tratando con algunos datos dentro de este Jupyter Notebook. La primera parte de código que ejecutará se encuentra en la celda de abajo. Puede presionar el botón *play* o presionar de manera más eficiente *shift + enter* en su teclado para ejecutar el código." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": {}, "colab_type": "code", "executionInfo": { "elapsed": 620, "status": "ok", "timestamp": 1598914214625, "user": { "displayName": "Rodrigo Chi", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjZhQsPZWT1SuS1mPdDTVpP6w0ON1iS36_FM3uQRA=s64", "userId": "15993798166308231899" }, "user_tz": 420 }, "id": "KtjYOL6XHOnf" }, "outputs": [], "source": [ "#This cell is a code cell. It is where we can type code that can be executed.\n", "#The hashtag at the start of this line makes it so that this text is a comment not code. \n", "\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ymrcB4r-HOnj" }, "source": [ "La razón por la que ejecutamos el código ```import pandas as pd``` es para poder usar las funciones de la libreria ```pandas```, que proporciona estructuras de datos y herramientas de análisis de datos que son realmente útiles. Una de las cosas fantásticas de realizar análisis de datos en Python es la disponibilidad de excelentes herramientas de análisis como ```pandas```. Algo frustrante puede ser aprender a usar estas diversas herramientas y saber cuando usarlas. Te sentirás cada vez más cómodo con estas herramientas a medida que avanza el semestre." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "KUTEgTghHOnj" }, "source": [ "# Buscando tu cumplemoto\n", "\n", "Tu cumple-moto o terremoto de nacimiento es el terremoto de mayor magnitud que ocurrió el día en que naciste. En este ejercicio en clase buscaremos un catálogo de terremotos y encontraremos tu cumple-moto.\n", "\n", "Para hacerlo, podemos descargar datos del programa de peligro sísmico del Servicio Geológico de EE.UU. (USGS). https://earthquake.usgs.gov\n", "\n", "Vamos a utilizar una API (*Application programming interface*\n", ") que nos permite enviar una URL a la USGS y obtener información sobre terremotos para un conjunto de parámetros especificados.\n", "\n", "## Buscando el Cumplemoto de Rodrigo\n", "\n", "\n", "Hagámoslo primero para mi cumpleaños. Definiremos mi cumpleaños en formato año-mes-día y el día después de mi cumpleaños en formato año-mes-día para crear una URL que obtenga datos a partir de las 12 AM de mi cumpleaños y terminando a las 12 AM del día siguiente. Estamos poniendo las comillas ('') alrededor de las fechas para que sean *strings* (el tipo de datos de Python que es una secuencia de texto)." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": {}, "colab_type": "code", "executionInfo": { "elapsed": 652, "status": "ok", "timestamp": 1598914357177, "user": { "displayName": "Rodrigo Chi", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjZhQsPZWT1SuS1mPdDTVpP6w0ON1iS36_FM3uQRA=s64", "userId": "15993798166308231899" }, "user_tz": 420 }, "id": "-HeA5HZ7HOnk" }, "outputs": [], "source": [ "Rodrigo_birthday = '1989-08-18'\n", "day_after_Rodrigo_birthday = '1989-08-19'" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "U7BFiwLlHOnm" }, "source": [ "Lo que acabamos de hacer en el código anterior es definir la variable ```Rodrigo_birthday``` para que tenga el valor string de ''. Si ejecutamos una celda de código solo con esa variable, Jupyter mostrará la variable como salida del código." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "colab_type": "code", "executionInfo": { "elapsed": 707, "status": "ok", "timestamp": 1598914372799, "user": { "displayName": "Rodrigo Chi", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjZhQsPZWT1SuS1mPdDTVpP6w0ON1iS36_FM3uQRA=s64", "userId": "15993798166308231899" }, "user_tz": 420 }, "id": "4_pe6GXpHOnn", "outputId": "7b18b1d9-0837-4c92-b90a-1332653ea48c" }, "outputs": [ { "data": { "text/plain": [ "'1989-08-18'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rodrigo_birthday" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DUgcLTnxHOnt" }, "source": [ "Otra forma de ver la variable es decirle a Python que la imprima usando la función `` print() ``." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 699, "status": "ok", "timestamp": 1598914378926, "user": { "displayName": "Rodrigo Chi", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjZhQsPZWT1SuS1mPdDTVpP6w0ON1iS36_FM3uQRA=s64", "userId": "15993798166308231899" }, "user_tz": 420 }, "id": "nE0x9YOdHOnt", "outputId": "d60c063f-4e40-4266-be23-8638dfd381b2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1989-08-18\n" ] } ], "source": [ "print(Rodrigo_birthday)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "A7KmPXHAHOnw" }, "source": [ "### Definición de la URL del terremoto del cumpleaños de Rodrigo\n", "\n", "Para crear una URL que podamos enviar al USGS (y recuperar datos) necesitamos insertar estas fechas en el formato de URL de la API de terremotos del USGS. Definiremos standard_url como un string y luego agregaremos las fechas que se establecieron arriba para ser la hora de inicio y la hora de finalización." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "colab_type": "code", "executionInfo": { "elapsed": 925, "status": "ok", "timestamp": 1598914443082, "user": { "displayName": "Rodrigo Chi", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjZhQsPZWT1SuS1mPdDTVpP6w0ON1iS36_FM3uQRA=s64", "userId": "15993798166308231899" }, "user_tz": 420 }, "id": "K5icHhlbHOnw", "outputId": "d0b99155-69e2-4597-b28b-322787c491dd" }, "outputs": [ { "data": { "text/plain": [ "'https://earthquake.usgs.gov/fdsnws/event/1/query?format=csv&orderby=magnitude&starttime=1989-08-18&endtime=1989-08-19'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "standard_url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=csv&orderby=magnitude'\n", "\n", "Rodrigo_birthquake_url = standard_url + '&starttime=' + Rodrigo_birthday + '&endtime=' + day_after_Rodrigo_birthday\n", "\n", "Rodrigo_birthquake_url" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "2XFAaH4wHOnz" }, "source": [ "### Conseguir los terremotos del cumpleaños de Rodrigo\n", "\n", "Ahora tenemos una URL que podemos usar para obtener datos del USGS. Podemos usar las funciones `pandas` que importamos en la parte superior de este notebook para obtener estos datos. La forma estándar de usar ```pandas``` es importarlo con la abreviatura ```pd```. Usaremos la función ```pd.read_csv()``` que tomará los datos que provienen de la URL de USGS y los convertirá en un DataFrame. Un DataFrame es una estructura de datos con columnas de diferentes datos que se definen con los nombres de sus columnas." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "colab": {}, "colab_type": "code", "executionInfo": { "elapsed": 2102, "status": "ok", "timestamp": 1598914493751, "user": { "displayName": "Rodrigo Chi", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjZhQsPZWT1SuS1mPdDTVpP6w0ON1iS36_FM3uQRA=s64", "userId": "15993798166308231899" }, "user_tz": 420 }, "id": "E9ZvxWJHHOnz" }, "outputs": [], "source": [ "Rodrigo_birthday_earthquakes = pd.read_csv(Rodrigo_birthquake_url)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wQLP3p4pHOn2" }, "source": [ "Estos datos están ordenados por magnitud con los terremotos de mayor magnitud en la parte superior. Veamos las primeras 5 filas del DataFrame usando la función ```.head ()```." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 400 }, "colab_type": "code", "executionInfo": { "elapsed": 800, "status": "ok", "timestamp": 1598914530502, "user": { "displayName": "Rodrigo Chi", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjZhQsPZWT1SuS1mPdDTVpP6w0ON1iS36_FM3uQRA=s64", "userId": "15993798166308231899" }, "user_tz": 420 }, "id": "l8hRhAXWHOn3", "outputId": "f80b5cd1-f854-4f40-ccb4-343d8552de97" }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
timelatitudelongitudedepthmagmagTypenstgapdminrms...updatedplacetypehorizontalErrordepthErrormagErrormagNststatuslocationSourcemagSource
01989-08-18T03:46:26.020Z-55.053-27.84633.05.6mwNaNNaNNaN1.2...2016-11-09T23:16:34.755ZSouth Sandwich Islands regionearthquakeNaNNaNNaNNaNreviewedushrv
11989-08-18T00:14:32.540Z0.709126.22158.95.0mbNaNNaNNaN1.0...2014-11-07T00:44:43.430Z129 km W of Ternate, IndonesiaearthquakeNaN11.1NaN9.0reviewedusus
21989-08-18T20:00:51.100Z51.380175.64933.04.9mbNaNNaNNaN1.0...2014-11-07T00:44:43.492Z234 km SE of Attu Station, AlaskaearthquakeNaNNaNNaN31.0reviewedusus
31989-08-18T09:29:17.390Z44.446147.12033.04.8mbNaNNaNNaN0.8...2014-11-07T00:44:43.468Z78 km NNE of Shikotan, RussiaearthquakeNaNNaNNaN5.0reviewedusus
41989-08-18T07:43:23.400Z36.31971.026104.94.6mbNaNNaNNaN0.8...2014-11-07T00:44:43.454Z60 km SW of Ashk?sham, AfghanistanearthquakeNaN14.4NaN4.0reviewedusus
\n", "

5 rows × 22 columns

\n", "
" ], "text/plain": [ " time latitude longitude depth mag magType nst \\\n", "0 1989-08-18T03:46:26.020Z -55.053 -27.846 33.0 5.6 mw NaN \n", "1 1989-08-18T00:14:32.540Z 0.709 126.221 58.9 5.0 mb NaN \n", "2 1989-08-18T20:00:51.100Z 51.380 175.649 33.0 4.9 mb NaN \n", "3 1989-08-18T09:29:17.390Z 44.446 147.120 33.0 4.8 mb NaN \n", "4 1989-08-18T07:43:23.400Z 36.319 71.026 104.9 4.6 mb NaN \n", "\n", " gap dmin rms ... updated \\\n", "0 NaN NaN 1.2 ... 2016-11-09T23:16:34.755Z \n", "1 NaN NaN 1.0 ... 2014-11-07T00:44:43.430Z \n", "2 NaN NaN 1.0 ... 2014-11-07T00:44:43.492Z \n", "3 NaN NaN 0.8 ... 2014-11-07T00:44:43.468Z \n", "4 NaN NaN 0.8 ... 2014-11-07T00:44:43.454Z \n", "\n", " place type horizontalError depthError \\\n", "0 South Sandwich Islands region earthquake NaN NaN \n", "1 129 km W of Ternate, Indonesia earthquake NaN 11.1 \n", "2 234 km SE of Attu Station, Alaska earthquake NaN NaN \n", "3 78 km NNE of Shikotan, Russia earthquake NaN NaN \n", "4 60 km SW of Ashk?sham, Afghanistan earthquake NaN 14.4 \n", "\n", " magError magNst status locationSource magSource \n", "0 NaN NaN reviewed us hrv \n", "1 NaN 9.0 reviewed us us \n", "2 NaN 31.0 reviewed us us \n", "3 NaN 5.0 reviewed us us \n", "4 NaN 4.0 reviewed us us \n", "\n", "[5 rows x 22 columns]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rodrigo_birthday_earthquakes.head()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "xBHhvT5PHOn5" }, "source": [ "#### El cumplemoto de Rodrigo.\n", "\n", "El terremoto más grande del día que nací fue una magnitud (columna `mag`) 5.6 que ocurrió en el border entre Myanmar e India (columna `place`)." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "WojO-DdNHOn5" }, "source": [ "## Encontrando tu cumplemoto\n", "### Ingresa tu cumpleaños\n", "\n", "Ingrese su cumpleaños y la fecha siguiente en formato año-mm-dd." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "iGj9ydCwHOn6" }, "outputs": [], "source": [ "### Enter your birthday here within the quotes in year-mm-dd format\n", "\n", "your_birthday = ''\n", "day_after_your_birthday = ''" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "HqKeFqLHHOn8" }, "source": [ "#### Definición de la URL del terremoto de tu cumpleaños" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "colab_type": "code", "id": "jWsjiV_sHOn8", "outputId": "039c154f-a40d-4787-948e-64542b80e233" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" }, "text/plain": [ "'https://earthquake.usgs.gov/fdsnws/event/1/query?format=csv&orderby=magnitude&starttime=1995-11-23&endtime=1995-11-24'" ] }, "execution_count": 10, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "standard_url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=csv&orderby=magnitude'\n", "\n", "your_birthquake_url = standard_url + '&starttime=' + your_birthday + '&endtime=' + day_after_your_birthday\n", "\n", "your_birthquake_url" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DOmRIezHHOn_" }, "source": [ "#### Obteniendo los terremotos el día de tu cumpleaños\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "wjkjTvlIHOoA" }, "outputs": [], "source": [ "your_birthday_earthquakes = pd.read_csv(your_birthquake_url)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "OfK2xNiTHOoD" }, "source": [ "### Buscando en los terremotos del día de tu cumpleaños" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 162 }, "colab_type": "code", "executionInfo": { "elapsed": 604, "status": "error", "timestamp": 1598840987168, "user": { "displayName": "Rodrigo Chi", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjZhQsPZWT1SuS1mPdDTVpP6w0ON1iS36_FM3uQRA=s64", "userId": "15993798166308231899" }, "user_tz": 420 }, "id": "F8MD3JB4HOoD", "outputId": "8a5ff803-1eae-40cb-c54d-e48b278de540" }, "outputs": [ { "ename": "NameError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0myour_birthday_earthquakes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'your_birthday_earthquakes' is not defined" ] } ], "source": [ "your_birthday_earthquakes.head()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rcOYs8reHOoF" }, "source": [ "Cuál es la magnitud de tu cumple-moto? Llena el siguiente formulario: https://forms.gle/XRVCyFBmW9aFYs3XA" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "qluUSwSjUute" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "S1_enclases_cumplemoto_estudiantes.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }