{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "JUlmFrUN645V" }, "source": [ "# Importar csv a Python \n", "Leer archivos csv como pandas DataFrame. " ] }, { "cell_type": "markdown", "metadata": { "id": "6Apr8Bd7645f" }, "source": [ "## Caso1: separado por comas y con cabecera" ] }, { "cell_type": "markdown", "source": [ "Primero clonamos el repositorio." ], "metadata": { "id": "N4pAO7vsEk2r" } }, { "cell_type": "code", "source": [ "!git clone https://github.com/financieras/pyCourse.git" ], "metadata": { "id": "09OvL9-1EbF8", "outputId": "c4059415-43bc-48e0-b4ae-f085ff1b1648", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Cloning into 'pyCourse'...\n", "remote: Enumerating objects: 6487, done.\u001b[K\n", "remote: Counting objects: 100% (1086/1086), done.\u001b[K\n", "remote: Compressing objects: 100% (335/335), done.\u001b[K\n", "remote: Total 6487 (delta 827), reused 963 (delta 751), pack-reused 5401\u001b[K\n", "Receiving objects: 100% (6487/6487), 5.82 MiB | 10.99 MiB/s, done.\n", "Resolving deltas: 100% (4844/4844), done.\n" ] } ] }, { "cell_type": "markdown", "source": [ "Vemos en que directorio estamos." ], "metadata": { "id": "HjKqLMXLEn2A" } }, { "cell_type": "code", "source": [ "%pwd" ], "metadata": { "id": "PoJ31rO5EU4o", "outputId": "e572766c-9947-429f-84f8-dd6d0d774b60", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "execution_count": 4, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'/content'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 4 } ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true, "id": "C0EdpL3D645i", "outputId": "b1eae5ff-1421-4ee5-91df-6deb7591bcfd", "colab": { "base_uri": "https://localhost:8080/", "height": 363 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Aeropuerto CodigoIATA Pasajeros Mercancía Insular Año\n", "0 MADRID MAD 61.734.944 560.039.136 FALSO 1931\n", "1 BARCELONA BCN 52.688.455 176.797.909 FALSO 1927\n", "2 PALMA DE MALLORCA PMI 29.721.142 9.021.606 VERDADERO 1960\n", "3 MALAGA AGP 19.858.656 3.080.119 FALSO 1946\n", "4 ALICANTE ALC 15.048.240 4.032.300 FALSO 1951\n", "5 GRAN CANARIA LPA 13.261.228 19.727.786 VERDADERO 1933\n", "6 TENERIFE SUR TFS 11.168.707 2.193.378 VERDADERO 1978\n", "7 VALENCIA VLC 8.539.579 14.515.842 FALSO 1930\n", "8 IBIZA IBZ 8.155.626 1.434.695 VERDADERO 1949\n", "9 SEVILLA SVQ 7.544.357 9.891.790 FALSO 1946" ], "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", "
AeropuertoCodigoIATAPasajerosMercancíaInsularAño
0MADRIDMAD61.734.944560.039.136FALSO1931
1BARCELONABCN52.688.455176.797.909FALSO1927
2PALMA DE MALLORCAPMI29.721.1429.021.606VERDADERO1960
3MALAGAAGP19.858.6563.080.119FALSO1946
4ALICANTEALC15.048.2404.032.300FALSO1951
5GRAN CANARIALPA13.261.22819.727.786VERDADERO1933
6TENERIFE SURTFS11.168.7072.193.378VERDADERO1978
7VALENCIAVLC8.539.57914.515.842FALSO1930
8IBIZAIBZ8.155.6261.434.695VERDADERO1949
9SEVILLASVQ7.544.3579.891.790FALSO1946
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "
\n", "
\n" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "import pandas as pd # importamos pandas\n", "\n", "path = 'pyCourse/jupyter/excel/aeropuertos.csv' # establecemos la ruta del archivo\n", "pd.read_csv(path)" ] }, { "cell_type": "markdown", "metadata": { "id": "TdyKFV_k645n" }, "source": [ "## Caso2: separado por punto y coma" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true, "id": "xIf7zBQF645o", "outputId": "1802be82-a423-4000-bebd-c41efa8ed5d7", "colab": { "base_uri": "https://localhost:8080/", "height": 206 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Multirriesgo Seguro_Directo Cartera\n", "0 Hogar 3,8% 1,8%\n", "1 Comercio -2,5% 0,0%\n", "2 Comunidades 3,0% 1,5%\n", "3 Industrial 6,9% 0,0%\n", "4 Otros Multirriesgos 7,3% 0,0%" ], "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", "
MultirriesgoSeguro_DirectoCartera
0Hogar3,8%1,8%
1Comercio-2,5%0,0%
2Comunidades3,0%1,5%
3Industrial6,9%0,0%
4Otros Multirriesgos7,3%0,0%
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "
\n", "
\n" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "path = 'pyCourse/jupyter/excel/multiriesgo.csv' # establecemos la ruta del archivo\n", "\n", "pd.read_csv(path,\n", " sep = ';')" ] }, { "cell_type": "markdown", "metadata": { "id": "yRXNdCE2645q" }, "source": [ "## Caso3: sin nombre de columna" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "yA8HKOBS645v", "outputId": "8ab13939-3d8c-4f86-93e8-d6b5f51e5af4", "colab": { "base_uri": "https://localhost:8080/", "height": 269 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Resultado marzo2020 marzo2021\n", "0 Vida 548.34 649.28\n", "1 Automóviles 294.33 370.99\n", "2 Multirriesgos 60.25 -25.24\n", "3 Salud 125.13 138.93\n", "4 Resto No Vida 252.96 356.64\n", "5 Total No Vida 732.66 841.32\n", "6 Total Vida y No Vida 1.281.01 1.490.60" ], "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", "
Resultadomarzo2020marzo2021
0Vida548.34649.28
1Automóviles294.33370.99
2Multirriesgos60.25-25.24
3Salud125.13138.93
4Resto No Vida252.96356.64
5Total No Vida732.66841.32
6Total Vida y No Vida1.281.011.490.60
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "
\n", "
\n" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "path = 'pyCourse/jupyter/excel/resultados.csv' # establecemos la ruta del archivo\n", "\n", "resultados2020 = pd.read_csv(path,\n", " header = None,\n", " names = ['Resultado','marzo2020', 'marzo2021'])\n", "\n", "resultados2020 # esta es la variable a la que hemos asociado el DataFrame para luego tratarla" ] }, { "cell_type": "markdown", "metadata": { "id": "OHyyP9gl645y" }, "source": [ "![](https://github.com/financieras/pyCourse/blob/main/jupyter/PushPyll2.png?raw=1)" ] } ], "metadata": { "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.8.11" }, "colab": { "name": "0140_csv.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 0 }