There are None values indicating there is no value for the Last Month row within the dictionary a below. How would I be able to modify the pandas style format so that it could print table a and still place dollar signs and in front of the set columns?
import numpy as np
import pandas as pd
a = {'Timeframes': ['Entirety:',
'Last Month:',
'Three Months:',
'Six Months:',
'Last Year:',
'Last Two Years:'],
'Compounding With Lev': np.array([2398012.89, None, 90.07, 85.29,
620.39, 30611.48], dtype=object),
'Compounding With Seperate Levs': np.array([21165662669.71, None, 91.18, 107.54,
3004.87, 13287947.75], dtype=object),
'Adjusted Long Compounding Lev': np.array([3.25, None, 1.0, 1.0, 3.5, 4.75], dtype=object),
'Adjusted Short Compounding Lev': np.array([3.75, None, 1.0, 3.0, 1.0, 2.0], dtype=object),
'Non Compounding With Lev': np.array([3626.41, None, 89.95, 95.73,
1577.75, 1380.80], dtype=object),
'Non Compounding With Seperate Levs': np.array([5679.53, None, 91.15, 408.40,
1953.53, 2530.58], dtype=object),
'Adjusted Long NonCompounding Lev': np.array([4.25, None, 1.0, 1.0, 10.5, 4.25], dtype=object),
'Adjusted Short NonCompounding Lev': np.array([7.75, None, 1.0, 33.25, 1.0, 7.75], dtype=object)}
display(pd.DataFrame(a).style.format(formatter={'Compounding With Lev': '${:,.2f}',
'Compounding With Seperate Levs': '${:,.2f}',
'Non Compounding With Lev': '${:,.2f}',
'Non Compounding With Seperate Levs': '${:,.2f}'}))
Expected Output:
CodePudding user response:
import numpy as np
import pandas as pd
a = {'Timeframes': ['Entirety:',
'Last Month:',
'Three Months:',
'Six Months:',
'Last Year:',
'Last Two Years:'],
'Compounding With Lev': np.array([2398012.89, None, 90.07, 85.29,
620.39, 30611.48], dtype=float),
'Compounding With Seperate Levs': np.array([21165662669.71, None, 91.18, 107.54,
3004.87, 13287947.75], dtype=float),
'Adjusted Long Compounding Lev': np.array([3.25, None, 1.0, 1.0, 3.5, 4.75], dtype=float),
'Adjusted Short Compounding Lev': np.array([3.75, None, 1.0, 3.0, 1.0, 2.0], dtype=float),
'Non Compounding With Lev': np.array([3626.41, None, 89.95, 95.73,
1577.75, 1380.80], dtype=float),
'Non Compounding With Seperate Levs': np.array([5679.53, None, 91.15, 408.40,
1953.53, 2530.58], dtype=float),
'Adjusted Long NonCompounding Lev': np.array([4.25, None, 1.0, 1.0, 10.5, 4.25], dtype=float),
'Adjusted Short NonCompounding Lev': np.array([7.75, None, 1.0, 33.25, 1.0, 7.75], dtype=float)}
display(pd.DataFrame(a).style.format(formatter={'Compounding With Lev': '${:,.2f}',
'Compounding With Seperate Levs': '${:,.2f}',
'Non Compounding With Lev': '${:,.2f}',
'Non Compounding With Seperate Levs': '${:,.2f}'}, na_rep='None'))
By changing dtype=float we allow numpy to place nan values in the array. na_rep parameter is what the formatter will put when the formatting is not applicable.
To convert dictionary a in case your dictionary is read like that from a file and the arrays are already dtype=object:
for k, v in a.items():
if isinstance(v, np.ndarray):
a[k] = v.astype(float)

