Pandas: Subindexing dataframes: Copies vs views
假设我有一个数据帧
1 2 3 | import pandas as pd import numpy as np foo = pd.DataFrame(np.random.random((10,5))) |
我从我的数据子集创建另一个数据帧:
1 | bar = foo.iloc[3:5,1:4] |
号
你的答案就在熊猫的文档中:返回一个视图对一个副本。
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.
号
在您的示例中,
请参阅下面的代码示例以说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | In [1]: import pandas as pd ...: import numpy as np ...: foo = pd.DataFrame(np.random.random((10,5))) ...: In [2]: pd.__version__ Out[2]: '0.12.0.dev-35312e4' In [3]: np.__version__ Out[3]: '1.7.1' In [4]: # DataFrame has copy method ...: foo_copy = foo.copy() In [5]: bar = foo.iloc[3:5,1:4] In [6]: bar == foo.iloc[3:5,1:4] == foo_copy.iloc[3:5,1:4] Out[6]: 1 2 3 3 True True True 4 True True True In [7]: # Changing the view ...: bar.ix[3,1] = 5 In [8]: # View and DataFrame still equal ...: bar == foo.iloc[3:5,1:4] Out[8]: 1 2 3 3 True True True 4 True True True In [9]: # It is now different from a copy of original ...: bar == foo_copy.iloc[3:5,1:4] Out[9]: 1 2 3 3 False True True 4 True True True |