Veamos cómo exportar los Panda DataFrame.
import pandas as pd # importamos la librería Pandas
# Datos de aeropuertos
mad = ['MADRID', 'MAD', 61734944, 560039136, False, 1931]
bcn = ['BARCELONA', 'BCN', 52688455, 176797909, False, 1927]
pmi = ['PALMA DE MALLORCA', 'PMI', 29721142, 9021606, True, 1960]
agp = ['MALAGA', 'AGP', 19858656, 3080119, False, 1946]
alc = ['ALICANTE', 'ALC', 15048240, 4032300, False, 1951]
lpa = ['GRAN CANARIA', 'LPA', 13261228, 19727786, True, 1933]
tfs = ['TENERIFE SUR', 'TFS', 11168707, 2193378, True, 1978]
vlc = ['VALENCIA', 'VLC', 8539579, 14515842, False, 1930]
ibz = ['IBIZA', 'IBZ', 8155626, 1434695, True, 1949]
svq = ['SEVILLA', 'SVQ', 7544357, 9891790, False, 1946]
list_airport = [mad, bcn, pmi, agp, alc, lpa, tfs, vlc, ibz, svq]
list_airport
[['MADRID', 'MAD', 61734944, 560039136, False, 1931], ['BARCELONA', 'BCN', 52688455, 176797909, False, 1927], ['PALMA DE MALLORCA', 'PMI', 29721142, 9021606, True, 1960], ['MALAGA', 'AGP', 19858656, 3080119, False, 1946], ['ALICANTE', 'ALC', 15048240, 4032300, False, 1951], ['GRAN CANARIA', 'LPA', 13261228, 19727786, True, 1933], ['TENERIFE SUR', 'TFS', 11168707, 2193378, True, 1978], ['VALENCIA', 'VLC', 8539579, 14515842, False, 1930], ['IBIZA', 'IBZ', 8155626, 1434695, True, 1949], ['SEVILLA', 'SVQ', 7544357, 9891790, False, 1946]]
# crear el DataFrame a partir de listas
df_airport = pd.DataFrame(list_airport,
columns = ['Aeropuerto', 'CodigoIATA', 'Pasajeros', 'Mercancía', 'Insular', 'Año'])
df_airport
Aeropuerto | CodigoIATA | Pasajeros | Mercancía | Insular | Año | |
---|---|---|---|---|---|---|
0 | MADRID | MAD | 61734944 | 560039136 | False | 1931 |
1 | BARCELONA | BCN | 52688455 | 176797909 | False | 1927 |
2 | PALMA DE MALLORCA | PMI | 29721142 | 9021606 | True | 1960 |
3 | MALAGA | AGP | 19858656 | 3080119 | False | 1946 |
4 | ALICANTE | ALC | 15048240 | 4032300 | False | 1951 |
5 | GRAN CANARIA | LPA | 13261228 | 19727786 | True | 1933 |
6 | TENERIFE SUR | TFS | 11168707 | 2193378 | True | 1978 |
7 | VALENCIA | VLC | 8539579 | 14515842 | False | 1930 |
8 | IBIZA | IBZ | 8155626 | 1434695 | True | 1949 |
9 | SEVILLA | SVQ | 7544357 | 9891790 | False | 1946 |
Primero clonamos el repositorio.
!git clone https://github.com/financieras/pyCourse.git
Cloning into 'pyCourse'... remote: Enumerating objects: 6492, done. remote: Counting objects: 100% (1086/1086), done. remote: Compressing objects: 100% (335/335), done. remote: Total 6492 (delta 826), reused 964 (delta 751), pack-reused 5406 Receiving objects: 100% (6492/6492), 5.82 MiB | 11.02 MiB/s, done. Resolving deltas: 100% (4848/4848), done.
Vemos en que directorio estamos.
%pwd
'/content'
# Exportar DataFrame a Excel
df_airport.to_excel('pyCourse/jupyter/excel/data_airport.xlsx')
# Exportar sin la columna de índices
df_airport.to_excel('pyCourse/jupyter/excel/data_airport_noindex.xlsx',
index = False)
import pandas as pd # importamos la librería Pandas
# Exportar un df a csv
df_airport.to_csv('pyCourse/jupyter/excel/data_airport.csv')
# Exportar sin la columna de índices
df_airport.to_csv('pyCourse/jupyter/excel/data_airport_noindex.csv',
index = False)
# Exportar df a csv sin indice y con punto y coma
df_airport.to_csv('pyCourse/jupyter/excel/data_airport_noindex_pc.csv',
index = False,
sep = ';')
# Recomendación: forzar la exportación en UTF-8
df_airport.to_csv('pyCourse/jupyter/excel/data_airport_utf8.csv',
encoding = 'utf-8')