【发布时间】:2020-01-29 08:40:27
【问题描述】:
我有一个代码,我可以在其中打开一个电子表格,阅读它,然后将其保存在一个多维数组中并查找字符串匹配项。
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"
为什么会这样?
【问题讨论】:
标签: python arrays string pandas