首页 > 解决方案 > 循环遍历 excel 工作表 - 数组索引超出范围

问题描述

我有一个 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)中都可以正常工作。可能是什么问题呢?

标签: pythonexcelpandas

解决方案


推荐阅读