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

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.

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.

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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement