I want to give dict_val
dataframe a title that is placed in the center title
. I am also trying to separate the each 3 digits with a comma on the Numbers
column how would I be able to give a dataframe a title and style the a column as such and get the Expected Output below?
import pandas as pd
import numpy as np
title = 'Number and Numbers 2 Comparison'
numbers = np.array([123242737.4923,679754672.3849])
numbers2 = np.array([123523,467895])
dict_val = pd.DataFrame({'Numbers':numbers.round(2),'Numbers 2': numbers2})
display(dict_val)
Output:
Expected output:
Number and Numbers 2 Comparison
Numbers Numbers 2
0 123,242,737.49 123523
1 679,754,672.38 467895
CodePudding user response:
Use Styler.format
:
dict_val.style.format(formatter={'Numbers': '{:,.2f}'})
Create MultiIndex
and set styles for alignment to center:
text = 'Number and Numbers 2 Comparison'
dict_val.columns = pd.MultiIndex.from_product([[text], dict_val.columns])
print (dict_val)
Number and Numbers 2 Comparison
Numbers Numbers 2
0 1.232427e 08 123523
1 6.797547e 08 467895
css = [ {'selector': 'th.col_heading.level0', 'props': 'text-align: center;'}]
dict_val.style.set_table_styles(css, overwrite=False).format(formatter={(text, 'Numbers'): '{:,.2f}'})