如何在 pandas 中创建数据框视图? [英] How to create a view of dataframe in pandas?

查看:53
本文介绍了如何在 pandas 中创建数据框视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大数据框(10m行,40列,7GB内存).我想创建一个视图,以便为表达起来很复杂的视图取一个简写名称,而不会增加2-4 GB的内存使用量.换句话说,我宁愿输入:

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

比:

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.

我知道我可以将条件和列列表分配给变量(例如df.loc[cond, cols]),但是我通常很想知道是否可以创建数据框视图.

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.

修改:相关问题:

  • What rules does Pandas use to generate a view vs a copy?
  • Pandas: Subindexing dataframes: Copies vs views

推荐答案

您通常无法返回视图.

您的答案在于pandas文档: returning-a-view-与复制.

Your answer lies in the pandas docs: returning-a-view-versus-a-copy.

只要标签数组或布尔向量涉及到 索引操作,结果将是副本.带单标签/ 标量索引和切片,例如df.ix [3:6]或df.ix [:,'A'],视图 将返回.

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.

这篇关于如何在 pandas 中创建数据框视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆