Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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).

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'), index=['x', 'y'])

def test(df_t):
    df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'), index=['x', 'y'])
    df = df.append(df2)
    print(df)

test(df)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

>Solution :

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.

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'), index=['x', 'y'])

def test():  # <- df_t is useless now
    global df  # HERE
    df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'), index=['x', 'y'])
    df = df.append(df2)
    print(df)

test(df)

But the suggestion of @Neither is more pertinent:

def test(df_t):
    df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'), index=['x', 'y'])
    return df_t.append(df2)

test()

Output:

   A  B
x  1  2
y  3  4
x  5  6
y  7  8
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading