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)
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
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)
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
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)
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
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)
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)