如何在 Pandas 中创建数据框视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39972778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a view of dataframe in pandas?
提问by IanS
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:
我有一个大数据框(10m 行、40 列、7GB 内存)。我想创建一个视图,以便为一个难以表达的视图提供一个简写名称,而不需要再增加 2-4 GB 的内存使用量。换句话说,我宁愿输入:
df2
Than:
比:
df.loc[complicated_condition, some_columns]
The documentationstates 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.
该文件指出,在使用.loc
保证了设定值修改原始数据帧,但仍然没有保证为返回的对象是否.loc
是一个视图或复印件。
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.
我知道我可以将条件和列列表分配给变量(例如df.loc[cond, cols]
),但我通常很想知道是否可以创建数据框的视图。
Edit: Related questions:
编辑:相关问题:
采纳答案by Eran Yogev
You generally can't return a view.
您通常无法返回视图。
Your answer lies in the pandas docs: returning-a-view-versus-a-copy.
您的答案在 pandas 文档中: return-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.
每当索引操作中涉及标签数组或布尔向量时,结果将是一个副本。使用单标签/标量索引和切片,例如 df.ix[3:6] 或 df.ix[:, 'A'],将返回一个视图。
This answer was found in the following post: Link.
在以下帖子中找到了这个答案:链接。