In [1]:
# Importar las librerías a utilizar
import pandas as pd
import requests
In [2]:
# Definir la URL de la petición
url = "https://app.bde.es/bierest/resources/srdatosapp/listaSeries?idioma=es&series=DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL,DEEQ.N.ES.W1.S1.S1.T.B.SD._Z._Z._Z.EUR._T._X.N.ALL,DEEQ.N.ES.W1.S1.S1.T.B.OS._Z._Z._Z.EUR._T._X.N.ALL&rango=MAX"
In [3]:
# Hacer la petición y comprobar si fue correcta

response = requests.get(url)

if response.status_code == 200:
    data = response.json()
    print("Datos obtenidos correctamente")
else:
    print(f"Error en la llamada a la API: {response.status_code}")
Datos obtenidos correctamente
In [4]:
# Ver el JSON que devuelve la API (únicamente la estructura de la primera serie)
print(data[:1])
[{'serie': 'DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL', 'descripcion': 'BP. Bienes. Saldos', 'descripcionCorta': 'BP. Bienes', 'codFrecuencia': 'Q', 'decimales': 0, 'simbolo': 'M&euro;', 'informacion': [{'titulo': 'Nombre', 'descripcion': 'BP. Bienes'}, {'titulo': 'Descripción', 'descripcion': 'Estadísticas exteriores. Balanza de pagos. BP. Bienes. Saldos.'}, {'titulo': 'Unidades', 'descripcion': 'Millones de euros'}, {'titulo': 'Decimales', 'descripcion': '0'}, {'titulo': 'Número de observaciones', 'descripcion': '133'}, {'titulo': 'Primer valor', 'descripcion': '[1T 1993] -3.091 M&euro;'}, {'titulo': 'Último valor', 'descripcion': '[1T 2026] -11.867 M&euro;'}, {'titulo': 'Valor mínimo', 'descripcion': '[4T 2007] -26.301 M&euro;'}, {'titulo': 'Valor máximo', 'descripcion': '[2T 2020] 675 M&euro;'}, {'titulo': 'Fuente', 'descripcion': 'Banco de España'}, {'titulo': 'Series relacionadas (cuadro pdf)', 'descripcion': "<a href='https://www.bde.es/webbe/es/estadisticas/compartido/datos/pdf/a0206b.pdf'>Descarga el cuadro en formato pdf con series relacionadas</a>"}, {'titulo': 'Series relacionadas (archivo Excel)', 'descripcion': "<a href='https://www.bde.es/webbe/es/estadisticas/compartido/datos/xlsx/be0206.xlsx'>Descarga el archivo Excel con series relacionadas</a>"}], 'fechaInicio': '1993-01-01T09:15:00Z', 'fechaFin': '2026-01-01T09:15:00Z', 'fechas': ['2026-01-01T09:15:00Z', '2025-10-01T08:15:00Z', '2025-07-01T08:15:00Z', '2025-04-01T08:15:00Z', '2025-01-01T09:15:00Z', '2024-10-01T08:15:00Z', '2024-07-01T08:15:00Z', '2024-04-01T08:15:00Z', '2024-01-01T09:15:00Z', '2023-10-01T08:15:00Z', '2023-07-01T08:15:00Z', '2023-04-01T08:15:00Z', '2023-01-01T09:15:00Z', '2022-10-01T08:15:00Z', '2022-07-01T08:15:00Z', '2022-04-01T08:15:00Z', '2022-01-01T09:15:00Z', '2021-10-01T08:15:00Z', '2021-07-01T08:15:00Z', '2021-04-01T08:15:00Z', '2021-01-01T09:15:00Z', '2020-10-01T08:15:00Z', '2020-07-01T08:15:00Z', '2020-04-01T08:15:00Z', '2020-01-01T09:15:00Z', '2019-10-01T08:15:00Z', '2019-07-01T08:15:00Z', '2019-04-01T08:15:00Z', '2019-01-01T09:15:00Z', '2018-10-01T08:15:00Z', '2018-07-01T08:15:00Z', '2018-04-01T08:15:00Z', '2018-01-01T09:15:00Z', '2017-10-01T08:15:00Z', '2017-07-01T08:15:00Z', '2017-04-01T08:15:00Z', '2017-01-01T09:15:00Z', '2016-10-01T08:15:00Z', '2016-07-01T08:15:00Z', '2016-04-01T08:15:00Z', '2016-01-01T09:15:00Z', '2015-10-01T08:15:00Z', '2015-07-01T08:15:00Z', '2015-04-01T08:15:00Z', '2015-01-01T09:15:00Z', '2014-10-01T08:15:00Z', '2014-07-01T08:15:00Z', '2014-04-01T08:15:00Z', '2014-01-01T09:15:00Z', '2013-10-01T08:15:00Z', '2013-07-01T08:15:00Z', '2013-04-01T08:15:00Z', '2013-01-01T09:15:00Z', '2012-10-01T08:15:00Z', '2012-07-01T08:15:00Z', '2012-04-01T08:15:00Z', '2012-01-01T09:15:00Z', '2011-10-01T08:15:00Z', '2011-07-01T08:15:00Z', '2011-04-01T08:15:00Z', '2011-01-01T09:15:00Z', '2010-10-01T08:15:00Z', '2010-07-01T08:15:00Z', '2010-04-01T08:15:00Z', '2010-01-01T09:15:00Z', '2009-10-01T08:15:00Z', '2009-07-01T08:15:00Z', '2009-04-01T08:15:00Z', '2009-01-01T09:15:00Z', '2008-10-01T08:15:00Z', '2008-07-01T08:15:00Z', '2008-04-01T08:15:00Z', '2008-01-01T09:15:00Z', '2007-10-01T08:15:00Z', '2007-07-01T08:15:00Z', '2007-04-01T08:15:00Z', '2007-01-01T09:15:00Z', '2006-10-01T08:15:00Z', '2006-07-01T08:15:00Z', '2006-04-01T08:15:00Z', '2006-01-01T09:15:00Z', '2005-10-01T08:15:00Z', '2005-07-01T08:15:00Z', '2005-04-01T08:15:00Z', '2005-01-01T09:15:00Z', '2004-10-01T08:15:00Z', '2004-07-01T08:15:00Z', '2004-04-01T08:15:00Z', '2004-01-01T09:15:00Z', '2003-10-01T08:15:00Z', '2003-07-01T08:15:00Z', '2003-04-01T08:15:00Z', '2003-01-01T09:15:00Z', '2002-10-01T08:15:00Z', '2002-07-01T08:15:00Z', '2002-04-01T08:15:00Z', '2002-01-01T09:15:00Z', '2001-10-01T08:15:00Z', '2001-07-01T08:15:00Z', '2001-04-01T08:15:00Z', '2001-01-01T09:15:00Z', '2000-10-01T08:15:00Z', '2000-07-01T08:15:00Z', '2000-04-01T08:15:00Z', '2000-01-01T09:15:00Z', '1999-10-01T08:15:00Z', '1999-07-01T08:15:00Z', '1999-04-01T08:15:00Z', '1999-01-01T09:15:00Z', '1998-10-01T08:15:00Z', '1998-07-01T08:15:00Z', '1998-04-01T08:15:00Z', '1998-01-01T09:15:00Z', '1997-10-01T08:15:00Z', '1997-07-01T08:15:00Z', '1997-04-01T08:15:00Z', '1997-01-01T09:15:00Z', '1996-10-01T08:15:00Z', '1996-07-01T08:15:00Z', '1996-04-01T08:15:00Z', '1996-01-01T09:15:00Z', '1995-10-01T09:15:00Z', '1995-07-01T08:15:00Z', '1995-04-01T08:15:00Z', '1995-01-01T09:15:00Z', '1994-10-01T09:15:00Z', '1994-07-01T08:15:00Z', '1994-04-01T08:15:00Z', '1994-01-01T09:15:00Z', '1993-10-01T09:15:00Z', '1993-07-01T08:15:00Z', '1993-04-01T08:15:00Z', '1993-01-01T09:15:00Z'], 'valores': [-11867, -12125, -15050, -9343, -12448, -10713, -10362, -6423, -6359, -9312, -12042, -8531, -5166, -11259, -19133, -15237, -14586, -11888, -6847, -1568, -1000, -186, -2260, 675, -5259, -5474, -8842, -3734, -7145, -7305, -8831, -5951, -6163, -5237, -7092, -3423, -5436, -4647, -3575, -1802, -3719, -4869, -6802, -4558, -4446, -4801, -6489, -4744, -5225, -4372, -4664, -1264, -2312, -3918, -6834, -7790, -9434, -10663, -11016, -9883, -11514, -11876, -12134, -12986, -10968, -12099, -11257, -8499, -10057, -17677, -21123, -24491, -24589, -26301, -23827, -22509, -21403, -22648, -21366, -21692, -20796, -18703, -18629, -18033, -15875, -15710, -15272, -13747, -11750, -11714, -11459, -10039, -9419, -11206, -9752, -9367, -8498, -9323, -10869, -10819, -9213, -10440, -11092, -10396, -9419, -8725, -8607, -8013, -5811, -6837, -5624, -4281, -4175, -3866, -3443, -3679, -3327, -2906, -3770, -3724, -3397, -3668, -4623, -4467, -3395, -4013, -4259, -3685, -2279, -3217, -3763, -3635, -3091]}]
In [5]:
# Crear el data frame
df = pd.DataFrame(data)
df.head()
Out[5]:
serie descripcion descripcionCorta codFrecuencia decimales simbolo informacion fechaInicio fechaFin fechas valores
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos BP. Bienes Q 0 M&euro; [{'titulo': 'Nombre', 'descripcion': 'BP. Bien... 1993-01-01T09:15:00Z 2026-01-01T09:15:00Z [2026-01-01T09:15:00Z, 2025-10-01T08:15:00Z, 2... [-11867, -12125, -15050, -9343, -12448, -10713...
1 DEEQ.N.ES.W1.S1.S1.T.B.SD._Z._Z._Z.EUR._T._X.N... BP. Turismo y viajes. Saldos BP. Turismo y viajes Q 0 M&euro; [{'titulo': 'Nombre', 'descripcion': 'BP. Turi... 1993-01-01T09:15:00Z 2026-01-01T09:15:00Z [2026-01-01T09:15:00Z, 2025-10-01T08:15:00Z, 2... [13469, 12842, 24745, 19620, 13186, 12758, 239...
2 DEEQ.N.ES.W1.S1.S1.T.B.OS._Z._Z._Z.EUR._T._X.N... BP. Servicios no turísticos. Saldos BP. Servicios no turísticos Q 0 M&euro; [{'titulo': 'Nombre', 'descripcion': 'BP. Serv... 1993-01-01T09:15:00Z 2026-01-01T09:15:00Z [2026-01-01T09:15:00Z, 2025-10-01T08:15:00Z, 2... [11472, 12739, 10038, 10574, 9886, 9284, 7603,...
In [6]:
# Obtener la unidad de medida de cada serie

def get_unidad(info):
    return next(
        (item["descripcion"] for item in info if item["titulo"] == "Unidades"),
        None
    )

df["unidad"] = df["informacion"].apply(get_unidad)
In [7]:
# Expandir los array de fechas y valores
df_exploded = df.explode(['fechas', 'valores'])

# Renombrar columnas
df_exploded = df_exploded.rename(columns={
    'codFrecuencia': 'frecuencia',
    'fechas': 'fecha',
    'valores': 'valor'
})

# Formatear fecha
df_exploded["fecha"] = pd.to_datetime(df_exploded["fecha"]).dt.date

df_exploded.head()
Out[7]:
serie descripcion descripcionCorta frecuencia decimales simbolo informacion fechaInicio fechaFin fecha valor unidad
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos BP. Bienes Q 0 M&euro; [{'titulo': 'Nombre', 'descripcion': 'BP. Bien... 1993-01-01T09:15:00Z 2026-01-01T09:15:00Z 2026-01-01 -11867 Millones de euros
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos BP. Bienes Q 0 M&euro; [{'titulo': 'Nombre', 'descripcion': 'BP. Bien... 1993-01-01T09:15:00Z 2026-01-01T09:15:00Z 2025-10-01 -12125 Millones de euros
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos BP. Bienes Q 0 M&euro; [{'titulo': 'Nombre', 'descripcion': 'BP. Bien... 1993-01-01T09:15:00Z 2026-01-01T09:15:00Z 2025-07-01 -15050 Millones de euros
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos BP. Bienes Q 0 M&euro; [{'titulo': 'Nombre', 'descripcion': 'BP. Bien... 1993-01-01T09:15:00Z 2026-01-01T09:15:00Z 2025-04-01 -9343 Millones de euros
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos BP. Bienes Q 0 M&euro; [{'titulo': 'Nombre', 'descripcion': 'BP. Bien... 1993-01-01T09:15:00Z 2026-01-01T09:15:00Z 2025-01-01 -12448 Millones de euros
In [8]:
# Seleccionar las columnas deseadas
df_filtrado = df_exploded[['serie', 'descripcion', 'frecuencia', 'unidad', 'decimales', 'fecha', 'valor']]
df_filtrado.head()
Out[8]:
serie descripcion frecuencia unidad decimales fecha valor
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos Q Millones de euros 0 2026-01-01 -11867
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos Q Millones de euros 0 2025-10-01 -12125
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos Q Millones de euros 0 2025-07-01 -15050
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos Q Millones de euros 0 2025-04-01 -9343
0 DEEQ.N.ES.W1.S1.S1.T.B.G._Z._Z._Z.EUR._T._X.N.ALL BP. Bienes. Saldos Q Millones de euros 0 2025-01-01 -12448
In [9]:
# Exportar el data frame a Excel
df_filtrado.to_excel('df_api_bde_ejemplo_python.xlsx', index=False)