pandas 百分比格式的 XlsxWriter 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32977538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:59:27  来源:igfitidea点击:

XlsxWriter error for percent format

pythonpandasxlsxwriter

提问by Gabriel

I'm using Pandas and exporting data to excel using XlsxWriter. One of the data columns has floats and needs to be formatted as percent, so this is how I do it:

我正在使用 Pandas 并使用 XlsxWriter 将数据导出到 excel。其中一个数据列有浮点数,需要格式化为百分比,所以我是这样做的:

percent_fmt = workbook.add_format({'num_format': '0.00%'})
worksheet.set_column('E:E', percent_fmt)

After that the following error appears:

之后出现以下错误:

File "C:\Program Files\Anaconda\lib\site-packages\xlsxwriter\worksheet.py", line 4688, in _write_col_info / float(max_digit_width) * 256.0) / 256.0

TypeError: unsupported operand type(s) for *: 'Format' and 'int'

文件“C:\Program Files\Anaconda\lib\site-packages\xlsxwriter\worksheet.py”,第 4688 行,在 _write_col_info / float(max_digit_width) * 256.0) / 256.0

类型错误:* 不支持的操作数类型:“格式”和“整数”

What am I doing wrong here?

我在这里做错了什么?

回答by jmcnamara

You need to specify a width before the format or None if you don't want to adjust the width.

如果您不想调整宽度,则需要在格式之前指定宽度或 None。

worksheet.set_column('E:E', None, percent_fmt)