python - pandas to_excel() 使用 float_format 参数 --> ValueError : could not convert string to float

标签 python excel pandas xlsxwriter

我有一个包含异构数据的 pandas DataFrame。这意味着有些列是 float ,有些是字符串,等等。

我首先尝试通过调用 xlsxwriter 工作表级别 set_column() 来格式化列。方法,但似乎 to_excel() 正在使用它自己的格式对象格式化每个单独的单元格,因此列级格式被覆盖。

我正在尝试将 DataFrame 导出到 Excel 并利用记录的 float_format 参数 here .

代码:

writer = pd.ExcelWriter(path, engine='xlsxwriter')

ff = '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)'

df.to_excel(writer, "sheet_name", index=False, float_format=ff)

我在调用 to_excel 时遇到的异常:

$VE_DIR/lib/python2.7/site-packages/pandas/util/decorators.pyc in wrapper(*args, **kwargs)
     58                 else:
     59                     kwargs[new_arg_name] = old_arg_value
---> 60             return func(*args, **kwargs)
     61         return wrapper
     62     return _deprecate_kwarg

$VE_DIR/lib/python2.7/site-packages/pandas/core/frame.pyc in to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep)
   1228         formatted_cells = formatter.get_formatted_cells()
   1229         excel_writer.write_cells(formatted_cells, sheet_name,
-> 1230                                  startrow=startrow, startcol=startcol)
   1231         if need_save:
   1232             excel_writer.save()

$VE_DIR/lib/python2.7/site-packages/pandas/io/excel.pyc in write_cells(self, cells, sheet_name, startrow, startcol)
    785         style_dict = {}
    786 
--> 787         for cell in cells:
    788             num_format_str = None
    789             if isinstance(cell.val, datetime.datetime):

$VE_DIR/lib/python2.7/site-packages/pandas/core/format.pyc in get_formatted_cells(self)
   1729         for cell in itertools.chain(self._format_header(),
   1730                                     self._format_body()):
-> 1731             cell.val = self._format_value(cell.val)
   1732             yield cell
   1733 

$VE_DIR/lib/python2.7/site-packages/pandas/core/format.pyc in _format_value(self, val)
   1510                 val = self.inf_rep
   1511             elif self.float_format is not None:
-> 1512                 val = float(self.float_format % val)
   1513         return val
   1514 

ValueError: could not convert string to float: _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)

我假设 to_excel() 只会尝试将参数应用于浮点格式的列(甚至特定单元格),而不是应用于每条数据,所以我不确定我错过了什么。如果需要,我将发布重现错误的特定表格的清理版本,但我想也许有人会认识到我所面临的问题。

谢谢!

最佳答案

你的ff完全无效。看看这个:

val = float(self.float_format % val)

现在试试这个(在 ipython 或其他东西中):

'_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' % 7.2

你需要使用python的float格式,而不是excel

关于python - pandas to_excel() 使用 float_format 参数 --> ValueError : could not convert string to float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25690056/

相关文章:

python - 如何在 Gtk 中制作类似 Lollypop 的侧边栏?

excel - 像 Excel 的 "VLOOKUP"函数一样工作的 SAS 代码

VBA Excel - .Window 无法识别打开的 Excel 窗口

excel - 如何使用 Delphi(任何版本)仅将 ADOQuery 中的某些列导出到 Excel?

python - 从记录中用 Pandas 索引几个 csv 文件?

Python PANDAS 写入 csv : how to set decimal point (".", 或 ",")?

Python:将列值的行收集到一行

c++ - 来自 cython wiki 的 Hello World 不起作用

python - 带 Bokeh 的条形图 pandas Dataframe

python - 连接数据框中的列