0

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?

2
  • 1
    stackoverflow.com/questions/50868186/… Commented Jul 23, 2020 at 23:00
  • The .append() method returns a new DataFrame. You either need to return it from your log function, or make error_log a global variable. Commented Jul 23, 2020 at 23:00

1 Answer 1

2

df.append() doesn't modify the dataframe in place, it creates a new df. You're assigning that to the local variable df, not error_log.

The function should return the new df, and you can reassign the variable.

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

for index, row in projects.iterrows():
    error_log = log_error(error_log, row, 'The project was not created', 'Help')
Sign up to request clarification or add additional context in comments.

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.