5

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)

3 Answers 3

4

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)

Sign up to request clarification or add additional context in comments.

1 Comment

axis=0 is correct.
1

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

2 Comments

Traceback (most recent call last): File "c:/Py/Postgres/Vers3/xlogIND3.py", line 35, in <module> saida = saida.append(df) AttributeError: 'NoneType' object has no attribute 'append'
Sorry, fixed it. The problem was redefining saida at the end of each loop (should not be done)
0

It didn't work 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.