In [49]:
import tia.util.fmt as fmt
import pandas as pd
import numpy as np
from IPython.display import HTML
In [50]:
# formatters are callable and accept a variety of values
number = 123456.5
fmt.ThousandDollarsFormatter(number)
Out[50]:
In [51]:
array = [123456.5, -123456.5]
fmt.ThousandDollarsFormatter(array)
Out[51]:
In [52]:
series = pd.Series({'a': 123456.5, 'b': 123456.5})
fmt.ThousandDollarsFormatter(series)
Out[52]:
In [53]:
frame = pd.DataFrame({'c1': series, 'c2': series})
fmt.ThousandDollarsFormatter(frame)
Out[53]:
In [54]:
ndarray = frame.values
fmt.ThousandDollarsFormatter(ndarray)
Out[54]:
In [55]:
# heterogeneous data frame
f = pd.DataFrame({'k': [1001.2, -94551.12], 'f': [100.99, -94.12], 'pct': [.021, -.505]})
f
Out[55]:
In [56]:
# let the formatter guess - it uses all values to make decision unless you set method
fmt.DynamicNumberFormatter(f)
Out[56]:
In [57]:
# Want more granual so tell it to do it by col (all values per column considered)
fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(f)
Out[57]:
In [58]:
# If one of the percents is greater than one, the dynamic will default pct to float format
fc = f.copy()
fc.ix[1, 'pct'] = 1.02
fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(fc)
Out[58]:
In [59]:
# You can do each cell individually if you like
fmt.new_dynamic_formatter(method='cell', pcts=1, precision=2)(fc)
Out[59]:
In [60]:
# What if the data is by row
tf = f.T
tf
Out[60]:
In [61]:
# show dynamic formtter by column. it defaults to best fit for all column values
fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(f.T)
Out[61]:
In [62]:
# instead do it by row
fmt.new_dynamic_formatter(method='row', pcts=1, precision=2)(f.T)
Out[62]:
In [63]:
# use the formatters for easier HTML generation
fmts = {
'k': fmt.new_thousands_formatter(precision=1, commas=True, parens=False),
'f': fmt.new_float_formatter(precision=3, parens=True),
'pct': fmt.new_percent_formatter(precision=1, parens=True)
}
HTML(f.to_html(formatters=fmts))
Out[63]:
In [ ]: