{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "1e095521", "metadata": { "id": "1e095521" }, "source": [ "# Exportar los DataFrame de Pandas \n", "Veamos cómo exportar los Panda DataFrame." ] }, { "cell_type": "markdown", "id": "962388c9", "metadata": { "id": "962388c9" }, "source": [ "## Exportar a Excel" ] }, { "cell_type": "code", "execution_count": 1, "id": "74107392", "metadata": { "id": "74107392" }, "outputs": [], "source": [ "import pandas as pd # importamos la librería Pandas" ] }, { "cell_type": "code", "execution_count": 2, "id": "6c2bbd75", "metadata": { "id": "6c2bbd75", "outputId": "50c68f5c-8cbe-41a0-e1e7-ab98020a5ab2", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[['MADRID', 'MAD', 61734944, 560039136, False, 1931],\n", " ['BARCELONA', 'BCN', 52688455, 176797909, False, 1927],\n", " ['PALMA DE MALLORCA', 'PMI', 29721142, 9021606, True, 1960],\n", " ['MALAGA', 'AGP', 19858656, 3080119, False, 1946],\n", " ['ALICANTE', 'ALC', 15048240, 4032300, False, 1951],\n", " ['GRAN CANARIA', 'LPA', 13261228, 19727786, True, 1933],\n", " ['TENERIFE SUR', 'TFS', 11168707, 2193378, True, 1978],\n", " ['VALENCIA', 'VLC', 8539579, 14515842, False, 1930],\n", " ['IBIZA', 'IBZ', 8155626, 1434695, True, 1949],\n", " ['SEVILLA', 'SVQ', 7544357, 9891790, False, 1946]]" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "# Datos de aeropuertos\n", "mad = ['MADRID', 'MAD', 61734944, 560039136, False, 1931]\n", "bcn = ['BARCELONA', 'BCN', 52688455, 176797909, False, 1927]\n", "pmi = ['PALMA DE MALLORCA', 'PMI', 29721142, 9021606, True, 1960]\n", "agp = ['MALAGA', 'AGP', 19858656, 3080119, False, 1946]\n", "alc = ['ALICANTE', 'ALC', 15048240, 4032300, False, 1951]\n", "lpa = ['GRAN CANARIA', 'LPA', 13261228, 19727786, True, 1933]\n", "tfs = ['TENERIFE SUR', 'TFS', 11168707, 2193378, True, 1978]\n", "vlc = ['VALENCIA', 'VLC', 8539579, 14515842, False, 1930]\n", "ibz = ['IBIZA', 'IBZ', 8155626, 1434695, True, 1949]\n", "svq = ['SEVILLA', 'SVQ', 7544357, 9891790, False, 1946]\n", "\n", "list_airport = [mad, bcn, pmi, agp, alc, lpa, tfs, vlc, ibz, svq]\n", "list_airport" ] }, { "cell_type": "code", "execution_count": 3, "id": "afa299d5", "metadata": { "id": "afa299d5", "outputId": "b25d4268-cb65-4a9f-fd62-936d646e0dcf", "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 61734944 560039136 False 1931\n", "1 BARCELONA BCN 52688455 176797909 False 1927\n", "2 PALMA DE MALLORCA PMI 29721142 9021606 True 1960\n", "3 MALAGA AGP 19858656 3080119 False 1946\n", "4 ALICANTE ALC 15048240 4032300 False 1951\n", "5 GRAN CANARIA LPA 13261228 19727786 True 1933\n", "6 TENERIFE SUR TFS 11168707 2193378 True 1978\n", "7 VALENCIA VLC 8539579 14515842 False 1930\n", "8 IBIZA IBZ 8155626 1434695 True 1949\n", "9 SEVILLA SVQ 7544357 9891790 False 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
0MADRIDMAD61734944560039136False1931
1BARCELONABCN52688455176797909False1927
2PALMA DE MALLORCAPMI297211429021606True1960
3MALAGAAGP198586563080119False1946
4ALICANTEALC150482404032300False1951
5GRAN CANARIALPA1326122819727786True1933
6TENERIFE SURTFS111687072193378True1978
7VALENCIAVLC853957914515842False1930
8IBIZAIBZ81556261434695True1949
9SEVILLASVQ75443579891790False1946
\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": 3 } ], "source": [ "# crear el DataFrame a partir de listas\n", "df_airport = pd.DataFrame(list_airport,\n", " columns = ['Aeropuerto', 'CodigoIATA', 'Pasajeros', 'Mercancía', 'Insular', 'Año'])\n", "\n", "df_airport" ] }, { "cell_type": "markdown", "source": [ "Primero clonamos el repositorio." ], "metadata": { "id": "G-YZ_oW8Fqad" }, "id": "G-YZ_oW8Fqad" }, { "cell_type": "code", "source": [ "!git clone https://github.com/financieras/pyCourse.git" ], "metadata": { "id": "msvQTm3mFyZg", "outputId": "d67f92c4-f5c8-41ac-fd2e-ccdc8600f3e3", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "msvQTm3mFyZg", "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Cloning into 'pyCourse'...\n", "remote: Enumerating objects: 6492, 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 6492 (delta 826), reused 964 (delta 751), pack-reused 5406\u001b[K\n", "Receiving objects: 100% (6492/6492), 5.82 MiB | 11.02 MiB/s, done.\n", "Resolving deltas: 100% (4848/4848), done.\n" ] } ] }, { "cell_type": "markdown", "source": [ "Vemos en que directorio estamos." ], "metadata": { "id": "MS7hExxLFz5j" }, "id": "MS7hExxLFz5j" }, { "cell_type": "code", "source": [ "%pwd" ], "metadata": { "id": "rXlB8BfZF2rB", "outputId": "1d9afca2-2a2a-43ca-8380-f636178af31c", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "id": "rXlB8BfZF2rB", "execution_count": 7, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'/content'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 7 } ] }, { "cell_type": "code", "execution_count": 8, "id": "4950491a", "metadata": { "id": "4950491a" }, "outputs": [], "source": [ "# Exportar DataFrame a Excel\n", "df_airport.to_excel('pyCourse/jupyter/excel/data_airport.xlsx')" ] }, { "cell_type": "code", "execution_count": 10, "id": "3bbe0d14", "metadata": { "id": "3bbe0d14" }, "outputs": [], "source": [ "# Exportar sin la columna de índices\n", "df_airport.to_excel('pyCourse/jupyter/excel/data_airport_noindex.xlsx',\n", " index = False)" ] }, { "cell_type": "markdown", "id": "034c3546", "metadata": { "id": "034c3546" }, "source": [ "## Exportar a csv" ] }, { "cell_type": "code", "execution_count": 11, "id": "41bbb89e", "metadata": { "id": "41bbb89e" }, "outputs": [], "source": [ "import pandas as pd # importamos la librería Pandas\n", "\n", "# Exportar un df a csv\n", "df_airport.to_csv('pyCourse/jupyter/excel/data_airport.csv')" ] }, { "cell_type": "code", "execution_count": 13, "id": "7ecda6f1", "metadata": { "id": "7ecda6f1" }, "outputs": [], "source": [ "# Exportar sin la columna de índices\n", "df_airport.to_csv('pyCourse/jupyter/excel/data_airport_noindex.csv',\n", " index = False)" ] }, { "cell_type": "code", "execution_count": 14, "id": "546080d1", "metadata": { "id": "546080d1" }, "outputs": [], "source": [ "# Exportar df a csv sin indice y con punto y coma\n", "df_airport.to_csv('pyCourse/jupyter/excel/data_airport_noindex_pc.csv',\n", " index = False,\n", " sep = ';')" ] }, { "cell_type": "code", "execution_count": 15, "id": "ce24e022", "metadata": { "id": "ce24e022" }, "outputs": [], "source": [ "# Recomendación: forzar la exportación en UTF-8\n", "df_airport.to_csv('pyCourse/jupyter/excel/data_airport_utf8.csv',\n", " encoding = 'utf-8')" ] } ], "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": "0150_exportar_dataframe.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }