{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Propiedades loc, iloc, at e iat --- 5:51 min\n", "\n", "* 5:51 min | Última modificación: Octubre 6, 2021 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pandas es una librería de alto desempeño para el manejo y análisis de datos en Python ampliamente utilizada en Analítica y Ciencia de Datos, por lo que su dominio resulta fundamental. Pandas se especializa en estructuras \"tidy\", es decir, tablas de datos donde cada fila es un registro y cada columna es un atributo." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Este tutorial esta basado en https://es.hortonworks.com/tutorial/beginners-guide-to-apache-pig/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preparación" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "pd.set_option(\"display.notebook_repr_html\", False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Carga de los datos de los conductores" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " driverId ssn location certified \\\n", "name \n", "George Vetticaden 10 621011971 244-4532 Nulla Rd. N \n", "Jamie Engesser 11 262112338 366-4125 Ac Street N \n", "Paul Coddin 12 198041975 Ap #622-957 Risus. Street Y \n", "Joe Niemiec 13 139907145 2071 Hendrerit. Ave Y \n", "Adis Cesir 14 820812209 Ap #810-1228 In St. Y \n", "\n", " wage-plan \n", "name \n", "George Vetticaden miles \n", "Jamie Engesser miles \n", "Paul Coddin hours \n", "Joe Niemiec hours \n", "Adis Cesir hours " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Carga del archivo ddesde un repo en GitHub\n", "#\n", "drivers = pd.read_csv(\n", " \"https://raw.githubusercontent.com/jdvelasq/playground/master/datasets/drivers/drivers.csv\",\n", " sep=\",\", \n", " thousands=None,\n", " decimal=\".\",\n", ")\n", "\n", "drivers.set_index(\"name\", inplace=True)\n", "drivers.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selección de un grupo de filas" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " driverId ssn location certified \\\n", "name \n", "Paul Coddin 12 198041975 Ap #622-957 Risus. Street Y \n", "Joe Niemiec 13 139907145 2071 Hendrerit. Ave Y \n", "Adis Cesir 14 820812209 Ap #810-1228 In St. Y \n", "\n", " wage-plan \n", "name \n", "Paul Coddin hours \n", "Joe Niemiec hours \n", "Adis Cesir hours " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# iloc recibe únicamente enteros, por lo que \n", "# permite seleccionar elementos por posición.\n", "#\n", "drivers.iloc[2:5]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " driverId ssn location certified wage-plan\n", "name \n", "Joe Niemiec 13 139907145 2071 Hendrerit. Ave Y hours\n", "Rohit Bakshi 15 239005227 648-5681 Dui- Rd. Y hours\n", "Adis Cesir 14 820812209 Ap #810-1228 In St. Y hours" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "drivers.loc[\n", " [\n", " \"Joe Niemiec\",\n", " \"Rohit Bakshi\",\n", " \"Adis Cesir\",\n", " ]\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selección de un grupo de filas y columnas" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " ssn certified\n", "name \n", "Paul Coddin 198041975 Y\n", "Joe Niemiec 139907145 Y\n", "Adis Cesir 820812209 Y" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "drivers.iloc[2:5, [1, 3]]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " location certified wage-plan\n", "name \n", "Joe Niemiec 2071 Hendrerit. Ave Y hours\n", "Rohit Bakshi 648-5681 Dui- Rd. Y hours\n", "Adis Cesir Ap #810-1228 In St. Y hours" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rows = [\n", " \"Joe Niemiec\",\n", " \"Rohit Bakshi\",\n", " \"Adis Cesir\",\n", " ]\n", "\n", "columns = [\n", " \"location\", \n", " \"certified\",\n", " \"wage-plan\",\n", "]\n", "\n", "drivers.loc[rows, columns]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selección de un registro en particular" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "driverId 11\n", "ssn 262112338\n", "location 366-4125 Ac Street\n", "certified N\n", "wage-plan miles\n", "Name: Jamie Engesser, dtype: object" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "drivers.iloc[1]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "driverId 11\n", "ssn 262112338\n", "location 366-4125 Ac Street\n", "certified N\n", "wage-plan miles\n", "Name: Jamie Engesser, dtype: object" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "drivers.loc[\"Jamie Engesser\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selección de una celda en particular por posición" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'366-4125 Ac Street'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "drivers.iat[1, 2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selección de una celda en particular usando etiquetas" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2071 Hendrerit. Ave'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "drivers.at[\"Joe Niemiec\", \"location\"]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.7 ('.venv': venv)", "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.10.7" }, "toc-autonumbering": false, "toc-showmarkdowntxt": false, "vscode": { "interpreter": { "hash": "99683f87c1354598af39114791cc78b29c5617e49c4bfa4c5fb7492534eeeb87" } } }, "nbformat": 4, "nbformat_minor": 4 }