Home > Back-end >  How to replace pandas append with concat?
How to replace pandas append with concat?

Time:03-18

Can you help me replace append with concat in this code?

saida = pd.DataFrame()
for x, y in lCodigos.items():
try:
    df = consulta_bc(x)
    logging.info(f'Indice {y} lido com sucesso.')
except Exception as err:
    logging.error(err)
    logging.warning('Rotina Indice falhou!')
    exit()
df['nome'] = y
saida = saida.append(df)   
print(saida)

CodePudding user response:

Just save the "dataframe parts" using a list and use pd.concat on that list of dataframes at the end:

saida = list()  # Now use a list
for x, y in lCodigos.items():
    # ... your original code
    saida.append(df)
saida = pd.concat(saida)  # You can now create the dataframe
  • Related