I have a df with a list of projects:
projects = pd.DataFrame([['Project 1', 'AAA'], ['Project 2', 'BBB']], columns=['project_name', 'code'])
I have an empty df to store errors:
error_log = pd.DataFrame(columns=['Project', 'Error'])
I have an error logging function that should write a row to the empty df:
def log_error(df, df_row, friendly_text, error):
df = df.append({'Project':df_row['project_name'],
'Error': friendly_text + ' due to this error: ' + repr(error)},
ignore_index=True)
This line runs without error...
for index, row in projects.iterrows():
log_error(error_log, row, 'The project was not created', 'Help')
...but no rows are written to error_log
(i.e. print(error_log)
returns nothing).
If I put the guts of the function directly into the for loop like this...
for index, row in projects.iterrows():
error_log = error_log.append({'Project':row['project_name'],'Error': 'The project was not created due to this error: ' + 'help'},ignore_index=True)
...it successfully writes rows to error_log
.
Why are rows not being written to the empty df when the appending occurs within a function?