简体   繁体   English

在复制之后但在编辑复制之前编辑原始DataFrame会更改复制

[英]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] = valuename.attr = valuename.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.

相关问题 使用 .copy() 创建字典的副本,可以在不更改原始字典的情况下进行编辑 - Using .copy() to create a copy of a dictionary that can be editing without altering the original dictionary 编辑Pandas DataFrame副本时发生意外转换 - Unexpected transformation in pandas DataFrame while editing its copy 如何复制包含列表的 Pandas 数据框,以便将来对副本中列表的更改不会改变原始列表 - How can I copy a pandas dataframe that contains a list, so that future changes to the list in the copy will not alter the original one 复制列表,但进行以下更改: - making a copy of the list but with following changes: 修改列表以复制另一个列表而不复制原始列表 - Modifying a list to copy another without making a copy of the original python:更改列表的副本会更改原始副本吗? - python: mutating the copy of a list changes the original? Pandas copy 正在更改原始数据帧,即使使用 copy(deep=True) - Pandas copy is changing the original dataframe, even with copy(deep=True) 一个pandas dataframe的深拷贝的列名更改 更改原始dataframe的列名 - Column name change of a deep copy of a pandas dataframe changes the column name of the original dataframe 如何在不制作副本的情况下缩小熊猫DataFrame的大小? - how to downsize a pandas DataFrame without making a copy? 用修改后的.copy()行替换原始DataFrame的行:将.copy()结果与原始DataFrame合并 - Replacing rows of original DataFrame with modified .copy() rows: Merging .copy() results with Original DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM