[英]Editing Original DataFrame After Making a Copy but Before Editing the Copy Changes the Copy
I am trying to understand how copying a pandas data frame works. 我试图了解如何复制pandas数据框。 When I assign a copy of an object in python I am not used to changes to the original object affecting copies of that object.
当我在python中分配对象的副本时,我不习惯更改影响该对象副本的原始对象。 For example:
例如:
x = 3
y = x
x = 4
print(y)
3
While x
has subsequently been changed, y remains the same. 虽然
x
随后被更改,但y保持不变。 In contrast, when I make changes to a pandas df
after assigning it to a copy df1
the copy is also affected by changes to the original DataFrame. 相反,当我将pandas
df
分配给副本df1
后对其进行更改时,副本也会受到原始DataFrame更改的影响。
import pandas as pd
import numpy as np
def minusone(x):
return int(x) - 1
df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
df1 = df
print(df1['A'])
0 10
1 20
2 30
3 40
4 50
Name: A, dtype: int64
df['A'] = np.vectorize(minusone)(df['A'])
print(df1['A'])
0 9
1 19
2 29
3 39
4 49
Name: A, dtype: int64
The solution appears to be making a deep copy with copy.deepcopy()
, but because this behavior is different from the behavior I am used to in python I was wondering if someone could explain what the reasoning behind this difference is or if it is a bug. 解决方案似乎是使用
copy.deepcopy()
进行深层复制,但是因为这种行为与我在python中习惯的行为不同,我想知道是否有人可以解释这种差异背后的原因是什么,或者它是否是错误。
In your first example, you did not make a change to the value of x
. 在第一个示例中,您没有更改
x
的值。 You assigned a new value to x
. 您为
x
分配了一个新值。
In your second example, you did modify the value of df
, by changing one of its columns. 在第二个示例中,您通过更改其中一个列来修改
df
的值。
You can see the effect with builtin types too: 你也可以看到内置类型的效果:
>>> x = []
>>> y = x
>>> x.append(1)
>>> y
[1]
The behavior is not specific to Pandas; 这种行为并非特定于熊猫; it is fundamental to Python.
它是Python的基础。 There are many, many questions on this site about this same issue, all stemming from the same misunderstanding.
关于同样的问题,这个网站上有很多很多问题,都源于同样的误解。 The syntax
语法
barename = value
does not have the same behavior as any other construct in Python . 与Python中的任何其他构造没有相同的行为 。
When using name[key] = value
, or name.attr = value
or name.methodcall()
, you may be mutating the value of the object referred to by name
, you may be copying something, etc. By using name = value
(where name
is a single identifier, no dots, no brackets, etc.), you never mutate anything, and never copy anything. 当使用
name[key] = value
或name.attr = value
或name.methodcall()
,您可能正在改变name
引用的对象的值,您可能正在复制某些内容等。使用name = value
(其中name
是单个标识符,没有点,没有括号等),你永远不会改变任何东西,也不会复制任何东西。
In your first example, you used the syntax x = ...
. 在第一个示例中,您使用了语法
x = ...
In your second example, you used the syntax df['A'] = ...
. 在第二个示例中,您使用了语法
df['A'] = ...
These are not the same syntax, so you can't assume they have the same behavior. 这些语法不同,因此您不能假设它们具有相同的行为。
The way to make a copy depends on the kind of object you're trying to copy. 制作副本的方式取决于您尝试复制的对象类型。 For your case, use
df1 = df.copy()
. 对于您的情况,请使用
df1 = df.copy()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.