1

I am trying to understand how to run a function that must take a data frame, append some rows and return the updated df. I tried to make a copy of the original data frame, make the append operation and then return a new df as a result of the function. However, this method does not work.

import pandas as pd

df1 = df = pd.DataFrame({"a":[1, 2, 3, 4],
                         "b":[5, 6, 7, 8]})
  

def append_function(d1):
    d2=d1.copy(deep=True)
    display(d2)
    df2 = pd.DataFrame({"a":[12, 22, 33],
                        "b":[51, 62, 73]})

    d2.append(df2)
    display(d2)
    return d2

df3=append_function(df1)
display(df3)

The answer is always the content of the original data frame.

Can someone please give me an explanation about that?

cs95
  • 379,657
  • 97
  • 704
  • 746
Giamma Fer
  • 65
  • 1
  • 8
  • Is it because you aren't assigning the result back to `df` after you call append? It does not operate in-place. See https://stackoverflow.com/questions/53924656/df-append-is-not-appending-to-the-dataframe – cs95 Oct 02 '21 at 02:55

0 Answers0