Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新对随后修改的附加值的更改_Python_Arrays_String_Pandas - Fatal编程技术网

Python 更新对随后修改的附加值的更改

Python 更新对随后修改的附加值的更改,python,arrays,string,pandas,Python,Arrays,String,Pandas,我有一个代码,我打开一个电子表格,读取它,并将其保存在多维数组中,然后查找字符串匹配 import pandas as pd import numpy as np file = pd.ExcelFile("File.xlsx") top100 = [] pub = [] ind = [] missed = [] for i in range(len(file.sheet_names)): year = 2005 + i df_aux = pd.read_excel(file

我有一个代码,我打开一个电子表格,读取它,并将其保存在多维数组中,然后查找字符串匹配

import pandas as pd
import numpy as np

file = pd.ExcelFile("File.xlsx")

top100 = []
pub = []
ind = []
missed = []

for i in range(len(file.sheet_names)):
    year = 2005 + i
    df_aux = pd.read_excel(file, str(year))
    top100.append(df_aux)
    df_aux2 = pd.read_excel("AnotherFile"+str(year+".xls")
    pub.append(df_aux2)
    ind_aux = []
    missed_aux = []
    df_aux2['Contributors'] = df_aux2['Contributors'].str.replace(" ",'')
    df_aux['Institution'] = df_aux['Institution'].str.replace(" ",'')    
    for j in range(len(df_aux2)):
        a = np.where(df_aux2['Contributors'][j] == df_aux['Institution'])[0]
        if len(a)>0:
            ind_aux.append(j)
        else:
            missed_aux.append(j)
    ind.append(ind_aux)
    missed.append(missed_aux)
代码的目的是在列表中查找匹配项。因为它们是字符串并且有一些问题,所以我删除了所有空格。我的理解是,这不应该改变已经附加的内容,但是如果我打印例如pub[0],我会得到所有没有空格的单词

print(pub[0]['Contributors'])
"Therearenospaces"

为什么会发生这种情况?

之所以会发生这种情况,是因为使用
pub.append(df_aux2)
,实际上没有两个不同的值。赋值只是将引用复制到一个值,而不是实际的数据帧,因此附加的
df_aux2
和形式的
df_aux2
在赋值后引用相同的变量。 要真正复制一个列表,可以使用
list.copy()
方法,我相信这是从Python 3.3开始提供的。如果我没有弄错的话,这应该可以做到:

new_pub = pub.append(df_aux2).copy()

根据Celius Stingher的回答,以下方法有效:

new_pub = pub.copy()

我应该把它放在代码的什么地方?我试图替换
pub.append(df_aux2)
,但我得到了以下错误:
AttributeError:“NoneType”对象没有属性“copy”
将编辑我的答案以考虑这种情况,谢谢