从pandas ExcelWriter对象读取表格到数据框

时间:2016-04-07 21:44:28

标签: python pandas openpyxl

我有一个带有多张工作表的pandas ExcelWriter对象,这些工作表是从excel电子表格加载的。我现在想把一张表作为数据框读出来。我知道我可以从原始的Excel电子表格中读取它,但我想从ExcelWriter对象中检索数据。

使用openpyxl

加载工作表
testwriter = pd.ExcelWriter(filename, engine = 'openpyxl')
testwriter.book = openpyxl.load_workbook(filename)
testwriter.sheets = dict((ws.title, ws) for ws in book.worksheets)
dataframe = pd.DataFrame(testwriter.sheets['sheetname']) ???

1 个答案:

答案 0 :(得分:0)

将现有的openpyxl工作表转换为Pandas数据帧非常简单。虽然版本2.4将为此提供最佳支持,但所有版本的原理都是相同的:循环遍历工作表中的行并返回单元格值。

请参阅openpyxl 2.4 documentation for working with Pandas了解一些想法。

顺便说一句。无需创建testwriter.sheets字典:使用`book [' sheetname']。