{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"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",
" Aeropuerto | \n",
" CodigoIATA | \n",
" Pasajeros | \n",
" Mercancía | \n",
" Insular | \n",
" Año | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" MADRID | \n",
" MAD | \n",
" 61.734.944 | \n",
" 560.039.136 | \n",
" FALSO | \n",
" 1931 | \n",
"
\n",
" \n",
" 1 | \n",
" BARCELONA | \n",
" BCN | \n",
" 52.688.455 | \n",
" 176.797.909 | \n",
" FALSO | \n",
" 1927 | \n",
"
\n",
" \n",
" 2 | \n",
" PALMA DE MALLORCA | \n",
" PMI | \n",
" 29.721.142 | \n",
" 9.021.606 | \n",
" VERDADERO | \n",
" 1960 | \n",
"
\n",
" \n",
" 3 | \n",
" MALAGA | \n",
" AGP | \n",
" 19.858.656 | \n",
" 3.080.119 | \n",
" FALSO | \n",
" 1946 | \n",
"
\n",
" \n",
" 4 | \n",
" ALICANTE | \n",
" ALC | \n",
" 15.048.240 | \n",
" 4.032.300 | \n",
" FALSO | \n",
" 1951 | \n",
"
\n",
" \n",
" 5 | \n",
" GRAN CANARIA | \n",
" LPA | \n",
" 13.261.228 | \n",
" 19.727.786 | \n",
" VERDADERO | \n",
" 1933 | \n",
"
\n",
" \n",
" 6 | \n",
" TENERIFE SUR | \n",
" TFS | \n",
" 11.168.707 | \n",
" 2.193.378 | \n",
" VERDADERO | \n",
" 1978 | \n",
"
\n",
" \n",
" 7 | \n",
" VALENCIA | \n",
" VLC | \n",
" 8.539.579 | \n",
" 14.515.842 | \n",
" FALSO | \n",
" 1930 | \n",
"
\n",
" \n",
" 8 | \n",
" IBIZA | \n",
" IBZ | \n",
" 8.155.626 | \n",
" 1.434.695 | \n",
" VERDADERO | \n",
" 1949 | \n",
"
\n",
" \n",
" 9 | \n",
" SEVILLA | \n",
" SVQ | \n",
" 7.544.357 | \n",
" 9.891.790 | \n",
" FALSO | \n",
" 1946 | \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",
" Multirriesgo | \n",
" Seguro_Directo | \n",
" Cartera | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Hogar | \n",
" 3,8% | \n",
" 1,8% | \n",
"
\n",
" \n",
" 1 | \n",
" Comercio | \n",
" -2,5% | \n",
" 0,0% | \n",
"
\n",
" \n",
" 2 | \n",
" Comunidades | \n",
" 3,0% | \n",
" 1,5% | \n",
"
\n",
" \n",
" 3 | \n",
" Industrial | \n",
" 6,9% | \n",
" 0,0% | \n",
"
\n",
" \n",
" 4 | \n",
" Otros Multirriesgos | \n",
" 7,3% | \n",
" 0,0% | \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",
" Resultado | \n",
" marzo2020 | \n",
" marzo2021 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Vida | \n",
" 548.34 | \n",
" 649.28 | \n",
"
\n",
" \n",
" 1 | \n",
" Automóviles | \n",
" 294.33 | \n",
" 370.99 | \n",
"
\n",
" \n",
" 2 | \n",
" Multirriesgos | \n",
" 60.25 | \n",
" -25.24 | \n",
"
\n",
" \n",
" 3 | \n",
" Salud | \n",
" 125.13 | \n",
" 138.93 | \n",
"
\n",
" \n",
" 4 | \n",
" Resto No Vida | \n",
" 252.96 | \n",
" 356.64 | \n",
"
\n",
" \n",
" 5 | \n",
" Total No Vida | \n",
" 732.66 | \n",
" 841.32 | \n",
"
\n",
" \n",
" 6 | \n",
" Total Vida y No Vida | \n",
" 1.281.01 | \n",
" 1.490.60 | \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
}