熊猫阅读Excel函数将索引转换为列表

时间:2018-09-18 15:36:14

标签: python-3.x pandas tkinter openpyxl xlsxwriter

## Summary: Analyze the data in each sheet and get the result
def analyze_data(project, sheet):
    print(project_dict[project],'****'+sheet)

    ## Get data with specific finding type in validation sheet
    sheet_df = pd.read_excel(project_dict[project],sheet, na_values=['NA'])
    print(sheet_df['Feedback Report']=='S.No')
    # Get index of tables
    242 idx = sheet_df[sheet_df['Feedback Report']=='S.No'].index.tolist()[0]
    243 head = idx - 1

    245 header_df = sheet_df.iloc[0:head,:]
    246 sheet_df = sheet_df.iloc[idx:,:]


    ## Replace the header
    header = sheet_df.iloc[0]
    sheet_df.columns = header.tolist()
    sheet_df = sheet_df[1:]

    ####################################
    ## Get data from the time period 

上面的代码不是我写的,我应该为其编写一个完整的Windows可执行文件。我无法理解该代码在242行中试图做什么。

Exception in Tkinter callback
    Traceback (most recent call last):
      File 37-32\lib\tkinter\__init__.py", line 1702, in __call__
        return self.func(*args)
      File QA_Review_Reporting.py", line 751, in sync
        report.read(project_dict)
      File reports.py", line 705, in read
        process()
      File reports.py", line 749, in process
        get_valid_type(project)
      File reports.py", line 185, in get_valid_type
        counts = analyze_data(project, item)
      File reports.py", line 242, in analyze_data
        idx = sheet_df[sheet_df['Feedback Report']=='S.No'].index.tolist()[0]
    IndexError: list index out of range

1 个答案:

答案 0 :(得分:2)

正如我在评论中提到的,第242行正在将数据帧sheet_df过滤到'Feedback Report'列的值为'S.No'的行中。然后,它将过滤后的sheet_df数据帧的相应索引返回到列表,并通过[0]获取该列表中的第一个元素。

例如:

sheet_df = pd.DataFrame([['No', 1, 2, 3], ['S.No', 4, 5, 6], ['S.No', 7, 8, 9], ['Yes', 10, 11, 12]], columns=['Feedback Report', 'Val 1', 'Val 2', 'Val 3'])

哪种产量:

  Feedback Report  Val 1  Val 2  Val 3
0              No      1      2      3
1            S.No      4      5      6
2            S.No      7      8      9
3             Yes     10     11     12

通过sheet_df[sheet_df['Feedback Report']=='S.No']过滤数据框将返回:

  Feedback Report  Val 1  Val 2  Val 3
1            S.No      4      5      6
2            S.No      7      8      9

然后获取索引并发送tolist()

[1, 2]

最后,通过[0]提取第一个元素以返回:

1