1

I have a pandas DataFrame with heterogenous data. That means that some columns are floats, some are strings, etc.

I first tried formatting the columns by calling the xlsxwriter worksheet-level set_column() method, but it appears that to_excel() is formatting each individual cell with it's own format object, so the column-level format is getting overridden.

I'm trying to export a DataFrame to Excel and take advantage of the float_format parameter documented here.

The code:

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

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

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

The Exception I'm getting upon calling 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);_(* "-"??_);_(@_)

I would assume to_excel() would only try to apply the parameter to float-formatted columns (or even specific cells) rather than to every piece of data, so I'm not sure what I'm missing. If need be I'll post a cleaned version of the specific table that reproduces the error, but I thought perhaps someone would recognize what I'm facing.

Thank you!

1 Answer 1

3

your ff is completely invalid. look at this:

val = float(self.float_format % val)

now try this (in ipython or something):

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

you need to use the float format for python, not excel

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @acushner; that was a fundamental misunderstanding... I'm trying to specifically format with thousand-separators like this though, and it's not getting recognized. Any ideas there?
Thanks, but I want to do the formatting in pandas so that the float is formatted that way in the resulting spreadsheet. Is there a way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.