Good day.

Please explain why you need the deep option when copying the data frame to Pandas?

The documentation is somehow vaguely written, it only underlines that for dataframes, deep copying is not an analogue of deep copying of ordinary python objects. But then how do the options deep=True and deep=False differ deep=False I did not understand.

If I just need to take one source data frame and get a copy of it, so that I can modify the source code and the copy independently of each other - what value of the deep option should I specify?

    1 answer 1

    In order to be sure that the data of your frames are independent, it is better to use DataFrame.copy(deep=True) ( deep=True - by default).

    Example:

     In [11]: df = pd.DataFrame(np.arange(9).reshape(3,3), columns=list('abc')) In [12]: df Out[12]: abc 0 0 1 2 1 3 4 5 2 6 7 8 In [13]: df2 = df.copy(deep=True) In [15]: df2.loc[:, 'a'] = 100 In [16]: df2 Out[16]: abc 0 100 1 2 1 100 4 5 2 100 7 8 In [17]: df Out[17]: abc 0 0 1 2 1 3 4 5 2 6 7 8 

    PS in Pandas 0.20.1+, I never managed to reproduce the problem of "dependent" frames ...

    • That is, if I want to work with copies, as with independent objects, it’s best to simply not specify this option? - Xander
    • @ Alexander, yes, either do not indicate, or indicate clearly deep=True - this is a matter of taste ... - MaxU