Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas dataframe, copy by value

Tags:

python

pandas

I noticed a bug in my program and the reason it is happening is because it seems that pandas is copying by reference a pandas dataframe instead of by value. I know immutable objects will always be passed by reference but pandas dataframe is not immutable so I do not see why it is passing by reference. Can anyone provide some information?

Thanks! Andrew

like image 306
Andrew Avatar asked Nov 16 '12 15:11

Andrew


People also ask

How do you copy values from a DataFrame in Python?

Pandas DataFrame copy() Method The copy() method returns a copy of the DataFrame. By default, the copy is a "deep copy" meaning that any changes made in the original DataFrame will NOT be reflected in the copy.

How do you copy values in a data frame?

If you want to make an explicit copy of a pandas object, try new_frame = frame. copy() .

How do I copy a pandas DataFrame?

To copy Pandas DataFrame, use the copy() method. The DataFrame. copy() method makes a copy of the provided object's indices and data. The copy() method accepts one parameter called deep, and it returns the Series or DataFrame that matches the caller.


1 Answers

All functions in Python are "pass by reference", there is no "pass by value". If you want to make an explicit copy of a pandas object, try new_frame = frame.copy().

like image 110
Wes McKinney Avatar answered Oct 17 '22 16:10

Wes McKinney