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?

OverflowingTheGlass
  • 2,324
  • 1
  • 27
  • 75
  • 1
    https://stackoverflow.com/questions/50868186/append-dataframe-inside-function – BENY Jul 23 '20 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. – Craig Jul 23 '20 at 23:00

1 Answers1

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')
Barmar
  • 741,623
  • 53
  • 500
  • 612