我有一个17张纸的Excel,并且正在运行一个循环以从除第一张纸之外的所有纸中提取数据。所有工作表的结构都相同,但是当我得到以下错误时,当我的i = 6时发生了错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-244-ea6f45e973a4> in <module>()
4 df_1 = pd.read_excel(r'C:\Users\filippo.sebastio\OneDrive - ELEVATE\Target\Target Download 28 Feb\Quantitative data\SCHAEFER_Putian ZhangSheng\zhangsheng -- RSAP Factory Metrics Tool- Hardcopy Form draft to publish 2018 12.xlsx', i , header = 4, index_col=1)
5 worksheet_1 = workbook.sheet_by_index(i)
----> 6 month = worksheet_1.cell(5,4).value
7 df_1 = df_1.drop(df_1.index[0])
8 df_1 = df_1.drop(df_1.index[-1])
~\Anaconda3\lib\site-packages\xlrd\sheet.py in cell(self, rowx, colx)
406 xfx = None
407 return Cell(
--> 408 self._cell_types[rowx][colx],
409 self._cell_values[rowx][colx],
410 xfx,
IndexError: array index out of range
这是我的循环
frame = pd.DataFrame()
list_ = []
for i in range(1,17):
df_1 = pd.read_excel(r'C:\Users\filippo.sebastio\OneDrive - ELEVATE\Target\Target Download 28 Feb\Quantitative data\SCHAEFER_Putian ZhangSheng\zhangsheng -- RSAP Factory Metrics Tool- Hardcopy Form draft to publish 2018 12.xlsx', i , header = 4, index_col=1)
worksheet_1 = workbook.sheet_by_index(i)
month = worksheet_1.cell(5,4).value
df_1 = df_1.drop(df_1.index[0])
df_1 = df_1.drop(df_1.index[-1])
df_1 = df_1.drop(df_1.columns[0], axis=1)
df_1 = df_1.dropna(axis=1, how='all')
for col in df_1.columns[0:3]:
df_1[col] = pd.to_numeric(df_1[col], errors='coerce')
df_1['mean'] = df_1.iloc[:, 0:3].mean(axis=1)
df_1 = df_1[[ 'mean']]
df_1_t = df_1.T
df_1_t['Month'] = month
df_1_t['Factory'] = worksheet_1.cell(3,2).value
df_1_t['Factory_id'] = worksheet_0.cell(3,2 ).value
df_1_t['Country'] = worksheet_0.cell(4,2 ).value
df_1_t['Consultant'] = worksheet_0.cell(5,2 ).value
list_.append(df_1_t)
list_
frame = pd.concat(list_)
frame
我检查了一下,工作表中的特定单元格不是空的(所有工作表都是相同的,并且填充类似)。而且,该循环在所有其他工作表上都可以正常工作-(1至5和7至17)。可能是什么问题?