Reputation: 3756
I have a large number of dataframes to output in Jupyter. The columns are a mix of strings, ints, and floats. The floats need mostly to be '%.2f'
, but a small subset require specific formatting -- mostly percentages and float-as-int.
The specific formatting is easy. But setting a default float format for Styling doesn't seem to exist.
display.float_format
doesn't cooperate with StylingStyler.format('{:.2f}'.format)
chokes on the strings/ints.Styler.set_precision()
uses general format, not float.pd.options.display.float_format
The only option I've found so far is to write a function that applies custom formats to some float columns, and the default format to all the others, while ignoring the strings and ints.
It seems awfully kludgy to have to explicitly write the same format to 90% of my columns. Is there a way to set a same-as-except default style by dtype?
Upvotes: 2
Views: 2971
Reputation: 81
For pandas, I find the most useful solution is to include the following line early on in the script:
pd.options.display.float_format = "{:,.0f}".format
Of course, the number of decimal digits as well as the "thousands" delimiter can be adjusted to meet your needs.
Health warning - By using this, all output (e.g., print, display, describe, etc.) will have the float output similarly adjusted. This can complicate problem-solving of your script as the numbers displayed may not be what is displayed.
Upvotes: 0
Reputation: 1483
Currently there is no such option for Styler (it is still under development so you can propose the idea). And as far as I get having Styler is requred for you. If so and if you need Styler only for views you can set new styling variable with default all-floats formatted:
float_cols = [c for c in df.dtypes.index if 'float' in str(df.dtypes[c])]
s = df.style.format(dict(zip(float_cols, [lambda x: "{:.2f}".format(x)]*10)))
Having this done you can use variable s for any further styling manipulation like:
s = s.applymap(...)
s = s.format(...)
etc...
You can also try to subclass pandas dataframe to have you own dataframe class with the predefined style as in the code above: Pandas subclassing guide
Upvotes: 3