Error "Valueerror: Length of value does not match length of index"

Asked

Viewed 1,361 times

0

I’m trying to find the address of stores "5aSec" in Brazil, but I keep getting error when I run this code

import requests
import json
import pandas as pd

dMun = pd.read_json('https://servicodados.ibge.gov.br/api/v1/localidades/municipios') 
dEndTotal = pd.DataFrame()
for iMun in range(len(dMun)):
    sCidade = dMun.loc[iMun,'nome']
    print(str(iMun) + ' - '+ dMun.loc[iMun,'nome'])
    sSigla = dMun.loc[iMun,'microrregiao']['mesorregiao']['UF']['sigla']
    r = requests.post('https://www.5asec.com.br/busca-lojas-endereco', data = {'endereco':'A, 1 {}/{}'.format(sCidade,sSigla)})
    jEnd = json.loads(r.text)
    dEnd = pd.DataFrame.from_records(jEnd['lojas'])
    print(dEnd)
    if len(dEnd) > 0:
        for sChave in jEnd['lojas'][0]['Endereco'].keys():
            dEnd[sChave] = []

        for i in range(len(dEnd)):
            for sChave in jEnd['lojas'][i]['Endereco'].keys():
                dEnd[sChave][i] = jEnd['lojas'][i]['Endereco'][sChave]
        dEndTotal = pd.concat([dEndTotal,dEnd],ignore_index=False).drop_duplicates().reset_index(drop=True)

That is the mistake:

0 - Alta Floresta D'Oeste
Empty DataFrame
Columns: []
Index: []
1 - Ariquemes
        CEP  Codigo CodigoExterno  ...  Telefone  TemEcommerce        Url
0  76870512     675        69004P  ...  35366864         False  ariquemes

[1 rows x 16 columns]
Traceback (most recent call last):

  File "<ipython-input-14-cd9a35514f7e>", line 1, in <module>
    runfile('C:/Users/vinis/OneDrive/Área de Trabalho/5aSec.py', wdir='C:/Users/vinis/OneDrive/Área de Trabalho')

  File "C:\Users\vinis\Anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\vinis\Anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 95, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/vinis/OneDrive/Área de Trabalho/5aSec.py", line 39, in <module>

  File "C:\Users\vinis\Anaconda2\lib\site-packages\pandas\core\frame.py", line 3370, in __setitem__
    self._set_item(key, value)

  File "C:\Users\vinis\Anaconda2\lib\site-packages\pandas\core\frame.py", line 3445, in _set_item
    value = self._sanitize_column(key, value)

  File "C:\Users\vinis\Anaconda2\lib\site-packages\pandas\core\frame.py", line 3630, in _sanitize_column
    value = sanitize_index(value, self.index, copy=False)

  File "C:\Users\vinis\Anaconda2\lib\site-packages\pandas\core\internals\construction.py", line 519, in sanitize_index
    raise ValueError('Length of values does not match length of index')

ValueError: Length of values does not match length of index

How shall I proceed to tidy up?

1 answer

1


You can convert the array to pd. Series and the missing values will be filled with null values.

dEnd[sChave] = pd.Series([])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.