0

I want to pass a DataFrame (df1) to a function. In the function the DataFrame gets changed. This change should not be applied to the original (df1). In c++ I can pass variables to functions with call_by_value. Unfortunately I don't get this pass in python.

def func1(df):
   #Change some values in the DataFrame

df1 = pd.DataFrame(some content...)

func1(df1) #df1 gets passed to func1

print(df1) #df1 does not have the same content after the function call as before the function call

The content in df1 should only be changed inside the function. When i print the DataFrame after i ran the function, it should have the same values like before

krat0r
  • 33
  • 3
  • Yes, of course, **python never uses call by value**. Or call by reference, for that matter. If you want an object to be copied, you need to do that **explicitly**. One simple solution is in your function, just do `df = df.copy()` as the first line. – juanpa.arrivillaga May 26 '21 at 10:21
  • @juanpa.arrivillaga: isn't the duplicate asking for the exact opposite? – Thomas Weller May 26 '21 at 10:24
  • @ThomasWeller yes, but it explains in detail Python evaluation strategy, which is *neither* call by reference nor call by value. There is only a single evaluation strategy, and you don't get to choose. – juanpa.arrivillaga May 26 '21 at 10:25

0 Answers0