Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 690
WhiteDillPickle Avatar asked Jul 17 '18 23:07

WhiteDillPickle


1 Answers

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.

E.g.

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

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

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

In [203]: f(df)

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

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

like image 175
aydow Avatar answered Nov 08 '22 21:11

aydow