Issue
I'm trying to set the table width using set_table_styles, but it doesn't work. I tried to find information about this in the documentation, but there is no example there. https://pandas.pydata.org/docs/user_guide/style.html
I looked into this issue, but it didn't help either. Set width to 100% (fill container) with pandas set_table_styles
Part of my code:
turnover = pd.pivot_table(data_for_turnover, columns=['Date'], values=['Turnover', 'Sales', 'Stocks'], aggfunc='sum')
table_style = {'selector': 'table','props': [('width', '100%')]}
turnover = turnover.style.format('{:.0f}')
turnover.set_table_styles([table_style]).to_html()
Next I display using Django, but the width (I also tried change border, margin) does not change. If I try to change the th or td styles, they are applied. Please help me find, if not a solution, then a reason.
Solution
I think if you need to set a fixed width
of an html table, you should use pixels rather then %
s.
table_style = {
# adjust the selectors if needed
"selector": "th.col_heading,td",
"props": [
("width", "100px"), # px instead of %
("text-align", "center"), # optional ?
]
}
(
turnover.style
.format(precision=0)
.set_table_styles([table_style])
# .to_html() # uncomment to make an html
)
Output (in Jupyter) :
Used input (to mock your pivot_table
):
import pandas as pd
import numpy as np
np.random.seed(1)
turnover = pd.DataFrame(
data=np.random.uniform(1, 1000, 12).reshape(3, 4),
index=["Turnover", "Sales", "Stocks"],
columns=["January", "February", "March", "April"]
)
print(turnover)
January February March April
Turnover 417.604983 720.604169 1.114260 303.030240
Sales 147.609135 93.246256 187.073951 346.215166
Stocks 397.370707 539.277917 419.775320 685.534281
Answered By - Timeless
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.