Pandas读取Excel函数将索引转换为列表

问题描述 投票:-1回答:1
## 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
python-3.x pandas tkinter openpyxl xlsxwriter
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.