0

I've been trying to get the list of addresses from a franchise at Brazil, but when I run the code, it starts, runs two cities and then it stops and appears "ValueError"

The code I've been trying to run is this:

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)

But its resulting on this error:

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-1-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 38, 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 can i fix this one? Thanks for the help, guys

And I'm sorry if the post isn't all correct

  • 1
    Welcome to Stack Overflow. It is not clear from your question what you are asking. "How can I fix this one?" is not a good question for Stack Overflow. Be sure and read [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and provide a [minimal, complete, verifiable example](https://stackoverflow.com/help/mcve) in your question so we can help you with a good answer. – Charlie Wallace Apr 14 '19 at 18:45
  • Possible duplicate of [ValueError: Length of values does not match length of index | Pandas DataFrame.unique()](https://stackoverflow.com/questions/42382263/valueerror-length-of-values-does-not-match-length-of-index-pandas-dataframe-u) – stovfl Apr 15 '19 at 12:28

0 Answers0