[SOLVED] Pandas DataFrame as an Argument to a Function – Python


Suppose a Pandas DataFrame is passed to a function as an argument. Then, does Python implicitly copy that DataFrame or is the actual DataFrame being passed in?

Hence, if I perform operations on the DataFrame within the function, will I be changing the original (because the references are still intact)?

I just want to know whether or not I should make a Deep Copy of my DataFrame before passing it into a function and operating on it.


If a function parameter is a mutable object (e.g. a DataFrame), then any changes you make in the function will be applied to the object.


In [200]: df = pd.DataFrame({1:[1,2,3]})

In [201]: df
0  1
1  2
2  3

In [202]: def f(frame):
     ...:     frame['new'] = 'a'

In [203]: f(df)

In [204]: df
   1 new
0  1   a
1  2   a
2  3   a

See this article for a good explanation on how Python passes function parameters.

Answered By – aydow

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *