Reputation: 16251
I have a large dataframe (10m rows, 40 columns, 7GB in memory). I would like to create a view in order to have a shorthand name for a view that is complicated to express, without adding another 2-4 GB to memory usage. In other words, I would rather type:
df2
Than:
df.loc[complicated_condition, some_columns]
The documentation states that, while using .loc
ensures that setting values modifies the original dataframe, there is still no guarantee as to whether the object returned by .loc
is a view or a copy.
I know I could assign the condition and column list to variables (e.g. df.loc[cond, cols]
), but I'm generally curious to know whether it is possible to create a view of a dataframe.
Edit: Related questions:
Upvotes: 26
Views: 22158
Reputation: 931
You generally can't return a view.
Your answer lies in the pandas docs: returning-a-view-versus-a-copy.
Whenever an array of labels or a boolean vector are involved in the indexing operation, the result will be a copy. With single label / scalar indexing and slicing, e.g. df.ix[3:6] or df.ix[:, 'A'], a view will be returned.
This answer was found in the following post: Link.
Upvotes: 14