Skip to content
Advertisement

Is it possible to append a df (which is declared outside) from a function

I am trying to append a pandas df which is outside of a function. Here, I want to append df2 (inside the function) with df (is located outside of the function).

JavaScript

I am getting UnboundLocalError: local variable 'df' referenced before assignment error (and that is expected because of the variable scope).

I have gone through this post. But, the only one answer of this post suggested append outside of the function (though df is declared inside of the function). However, I need to declare the df outside of the function and need to append with df2 inside the function.

If I try df.append(df2) instead of df = df.append(df2), program is not giving any error but getting only df as output (without append).

Advertisement

Answer

df is a declared out of the function. If you want to modify it you should declare it explicitly but in this case df_t is useless.

JavaScript

But the suggestion of @Neither is more pertinent:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement