html 使用pandas set_table_styles设置表格宽度

guykilcj  于 2024-01-04  发布在  其他
关注(0)|答案(1)|浏览(140)

我尝试使用set_table_styles设置表格宽度,但它不起作用。我试图在文档中查找有关此的信息,但那里没有示例。https://pandas.pydata.org/docs/user_guide/style.html
我调查了这个问题,但它也没有帮助。Set width to 100% (fill container) with pandas set_table_styles
我的部分代码:

  1. turnover = pd.pivot_table(data_for_turnover, columns=['Date'], values=['Turnover', 'Sales', 'Stocks'], aggfunc='sum')
  2. table_style = {'selector': 'table','props': [('width', '100%')]}
  3. turnover = turnover.style.format('{:.0f}')
  4. turnover.set_table_styles([table_style]).to_html()

字符串
接下来我使用Django显示,但宽度(我也尝试改变边框,边距)不改变.如果我尝试改变th或td样式,他们被应用.请帮助我找到,如果不是一个解决方案,然后一个原因.

uttx8gqw

uttx8gqw1#

我认为如果你需要设置一个固定的width的html表,你应该使用pixels而不是% s。

  1. table_style = {
  2. # adjust the selectors if needed
  3. "selector": "th.col_heading,td",
  4. "props": [
  5. ("width", "100px"), # px instead of %
  6. ("text-align", "center"), # optional ?
  7. ]
  8. }
  9. (
  10. turnover.style
  11. .format(precision=0)
  12. .set_table_styles([table_style])
  13. # .to_html() # uncomment to make an html
  14. )

字符串
输出(* 单位:字节 *):


的数据
Used input(to mock yourpivot_table):

  1. import pandas as pd
  2. import numpy as np
  3. np.random.seed(1)
  4. turnover = pd.DataFrame(
  5. data=np.random.uniform(1, 1000, 12).reshape(3, 4),
  6. index=["Turnover", "Sales", "Stocks"],
  7. columns=["January", "February", "March", "April"]
  8. )
  9. print(turnover)
  10. January February March April
  11. Turnover 417.604983 720.604169 1.114260 303.030240
  12. Sales 147.609135 93.246256 187.073951 346.215166
  13. Stocks 397.370707 539.277917 419.775320 685.534281

展开查看全部

相关问题