How to replace pandas append with concat?

Question:

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)
Asked By: Fcoatis

||

Answers:

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
Answered By: aaossa

saida= pd.concat([saida, df], axis= 1)

the above☝️ should work in place of the following
(set axis=0 if you’d like to place the dataframes ‘on top of each other’)

saida= saida.append(df)

Answered By: Con O'Leary

I didn’t worked in my system. After getting the list,

saidaAdd = pd.DataFrame(saida) # now you have the dataframe

FullFile = concat([‘original_file’, saidaAdd], axis =0)

is probably what was intended to build records

Answered By: Fernando Quintero
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.