我有一个代码,可以在其中打开电子表格,进行阅读,然后将其保存在多维数组中并查找字符串匹配项。

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()

10-08 08:19
查看更多