Say I have a dataframe
import pandas as pd
import numpy as np
foo = pd.DataFrame(np.random.random((10,5)))
and I create another dataframe from a subset of my data:
bar = foo.iloc[3:5,1:4]
does bar
hold a copy of those elements from foo
? Is there any way to create a view
of that data instead? If so, what would happen if I try to modify data in this view? Does Pandas provide any sort of copy-on-write mechanism?
Your answer lies in the pandas docs: returning-a-view-versus-a-copy.
In your example,
bar
is a view of slices offoo
. If you wanted a copy, you could have used thecopy
method. Modifyingbar
also modifiesfoo
. pandas does not appear to have a copy-on-write mechanism.See my code example below to illustrate: