When selecting a sub dataframe from a parent dataframe, I noticed that some programmers make a copy of the data frame using the .copy()
method.
Why are they making a copy of the data frame? What will happen if I don't make a copy?
When selecting a sub dataframe from a parent dataframe, I noticed that some programmers make a copy of the data frame using the .copy()
method.
Why are they making a copy of the data frame? What will happen if I don't make a copy?
In general it is safer to work on copies than on original data frames, except when you know that you won't be needing the original anymore and want to proceed with the manipulated version. Normally, you would still have some use for the original data frame to compare with the manipulated version, etc. Therefore, most people work on copies and merge at the end.
Because if you don't make a copy then the indices can still be manipulated elsewhere even if you assign the dataFrame to a different name.
For example:
func1 can modify df by modifying df2, so to avoid that:
The primary purpose is to avoid chained indexing and eliminate the
SettingWithCopyWarning
.The document said chained indexing should be avoided in Returning a view versus a copy. Here is a slightly modified example from that document:
Here the
aColumn
is a view and not a copy from the original DataFrame, so modifyingaColumn
will cause the originaldfc
be modified too. Next, if we index the row first:This time
zero_row
is a copy, so the originaldfc
is not modified.From these two examples above, we see it's ambiguous whether or not you want to change the original DataFrame. This is especially dangerous if you write something like the following:
This time it didn't work at all. Here we wanted to change
dfc
, but we actually modified an intermediate valuedfc.loc[0]
that is a copy and is discarded immediately. It’s very hard to predict whether the intermediate value likedfc.loc[0]
ordfc['A']
is a view or a copy, so it's not guaranteed whether or not original DataFrame will be updated. That's why chained indexing should be avoided, and pandas generates theSettingWithCopyWarning
for this kind of chained indexing update.Now is the use of
.copy()
. To eliminate the warning, make a copy to express your intention explicitly:Since you are modifying a copy, you know the original
dfc
will never change and you are not expecting it to change. Your expectation matches the behavior, then theSettingWithCopyWarning
disappears.Note, If you do want to modify the original DataFrame, the document suggests you use
loc
:It's necessary to mention that returning copy or view depends on kind of indexing.
The pandas documentation says:
This expands on Paul's answer. In Pandas, indexing a DataFrame returns a reference to the initial DataFrame. Thus, changing the subset will change the initial DataFrame. Thus, you'd want to use the copy if you want to make sure the initial DataFrame shouldn't change. Consider the following code:
You'll get:
In contrast, the following leaves df unchanged: