简体   繁体   English

pandas.read_excel() 仅通过 excel 工作表的第一行作为 dataframe ZD30145BA20E98D07372E41585D96Z 表

[英]pandas.read_excel() is only passing the first line of the excel sheet as a dataframe label

I am trying to read an Excel file using pandas.read_excel().我正在尝试使用 pandas.read_excel() 读取 Excel 文件。 The function is only picking up the very first row of the sheet I am passing and it is returning a 0 by 1 dataframe. function 只拾取我正在传递的工作表的第一行,它返回 0 x 1 dataframe。 Not only it just takes the first row, but it sets it as the dataframe label.它不仅占据第一行,而且将其设置为 dataframe label。 I tried passing the path as a handle but it did the same result我尝试将路径作为句柄传递,但结果相同

def read_excel_report(path):
    try:
        #handle = open(path)
        dictionary
        # representing a different worksheet.
        raw_excel = pd.read_excel(path, sheet_name=-1, engine='openpyxl')
        raw_excel = raw_excel.fillna('')

        print("data extracted")
        return raw_excel
    except Exception as e:
        print(e)

This happens with the older version of Pandas where Excel files are not created properly.旧版本的 Pandas 会发生这种情况,其中 Excel 文件未正确创建。

The best possible solution is to upgrade your Python version to 3.8 (or newer) and Pandas version to 1.2 (or newer).最好的解决方案是将 Python 版本升级到 3.8(或更高版本),将 Pandas 版本升级到 1.2(或更高版本)。 Here is Pandas changelog for your reference.这是 Pandas 更改日志供您参考。

If that is not possible then the workaround is to open and close the workbook.如果这是不可能的,那么解决方法是打开和关闭工作簿。 ie use openpyxl library methods load_workbook and close before using read_excel method of Pandas.即在使用read_excel的 read_excel 方法之前使用openpyxl库方法load_workbookclose This will work but it will add several seconds of delay.这会起作用,但会增加几秒钟的延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM