无法将数据透视表写入Excel文件

编程入门 行业动态 更新时间:2024-10-09 19:16:30
本文介绍了无法将数据透视表写入Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用pandas/openpyxl处理一个excel文件,然后创建一个数据透视表以添加到当前工作簿中的新工作表中.当我执行代码时,将创建新的工作表,但不会将数据透视表添加到工作表中.

I am using pandas/openpyxl to process an excel file and then create a pivot table to add to a new worksheet in the current workbook. When I execute my code, the new sheet gets created but the pivot table does not get added to the sheet.

这是我的代码:

worksheet2 = workbook.create_sheet() worksheet2.title = 'Sheet1' workbook.save(filename) excel = pd.ExcelFile(filename) df = excel.parse(sheetname=0) df1 = df[['Product Description', 'Supervisor']] table1 = pd.pivot_table(df1, index = ['Supervisor'], columns = ['Product Description'], values = ['Product Description'], aggfunc = [lambda x: len(x)], fill_value = 0) print table1 writer = pd.ExcelWriter(filename) table1.to_excel(writer, 'Sheet1') writer.save() workbook.save(filename)

当我打印出我的桌子时,我得到了:

When I print out my table I get this:

<lambda> \ Product Description EXPRESS 10:30 (doc) EXPRESS 10:30 (nondoc) Supervisor Building 0 1 Gordon 1 0 Pete 0 0 Vinny A 0 1 Vinny P 0 1 \ Product Description EXPRESS 12:00 (doc) EXPRESS 12:00 (nondoc) Supervisor Building 0 4 Gordon 1 2 Pete 1 0 Vinny A 1 1 Vinny P 0 1 Product Description MEDICAL EXPRESS (nondoc) Supervisor Building 0 Gordon 1 Pete 0 Vinny A 0 Vinny P 0

我希望数据透视表看起来像这样:(如果我的数据透视表代码不能使它看起来像这样,有人可以帮我使它看起来像吗?我不确定如何添加总计列.这与数据透视表的aggfunc部分有关吗?)

I would like the pivot table to look like this: (if my pivot table code won't make it look like this could someone help me make it look like that? I'm not sure how to add the grand total column. It has something to do with the aggfunc portion of the pivot table right?)

推荐答案

您不能执行此操作,因为openpyxl当前不支持数据透视表.有关更多信息,请参见 bitbucket/openpyxl/openpyxl/issues/295

You can't do this because openpyxl does not currently support pivot tables. See bitbucket/openpyxl/openpyxl/issues/295 for further information.

更多推荐

无法将数据透视表写入Excel文件

本文发布于:2023-07-09 21:07:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1088213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:透视   文件   数据   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!