0

So this is my first question here. Please let me know what style guides I failed to use. :)

I have a dataframe and within a function, I create a copy and work with this copy inside the function. Calling the function the first time works fine. The second time gives an error.

df_general = pd.read_excel("somedata.xlsx")

def my_func():

    df_within = df_general
    
    # some work with df_within, e.g. adding a column

    return

Reason I found: df_within is not set back to df_general - seems like the statement df_within = df_general is not taken up (but is taken up during first time using the function)

Any ideas? Thanks!

Josch
  • 1
  • _and within a function, I create a copy_ - no you dont. Use https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html – Patrick Artner Oct 25 '20 at 20:55

1 Answers1

0

I'm not sure I've quite understood your purpose.

If you are trying to make a copy of it so df_within changes wouldn't affect df_general you can use panda's copy() method as follows:

df_general = pd.read_excel("somedata.xlsx")

def my_func():

    df_within = df_general.copy()  # This will create a non-referenced copy by allocating new memory.

    # some work with df_within, e.g. adding a column

    return

Also you've stated you have an error the second time, might be a good idea to show us with that error was.

OmerM25
  • 243
  • 2
  • 13
  • That is exactly what my mistake was. thanks a lot. Also found a more general guide here for everybody else: https://stackoverflow.com/questions/27673231/why-should-i-make-a-copy-of-a-data-frame-in-pandas – Josch Oct 26 '20 at 07:17
  • @Josch I'm glad it helped :) Make sure you mark it as the correct answer for other people with similar problem to know. – OmerM25 Oct 27 '20 at 11:57