我正在尝试避免在调用.to_html()之后更改pandas DataFrame HTML。
我已经找到了导出到XLSX时如何自动调整DataFrame的方法,但是HTML存在问题。
import pandas as pd
from collections import OrderedDict
df = pd.DataFrame.from_dict(OrderedDict({'month': {('Tech 1', 2018): 'December', ('Tech 1', 2019): 'January',
('Tech 2', 2018): 'December', ('Tech 2', 2019): 'January',
('Tech 3', 2018): 'December', ('Tech 3', 2019): 'January',
('Tech 4', 2018): 'December', ('Tech 4', 2019): 'January'},
'new': {('Tech 1', 2018): 26, ('Tech 1', 2019): 17,
('Tech 2', 2018): 24, ('Tech 2', 2019): 12, ('Tech 3', 2018): 0,
('Tech 3', 2019): 4, ('Tech 4', 2018): 12, ('Tech 4', 2019): 14},
'complete': {('Tech 1', 2018): -36, ('Tech 1', 2019): 0,
('Tech 2', 2018): -111, ('Tech 2', 2019): -20, ('Tech 3', 2018): -9,
('Tech 3', 2019): 0, ('Tech 4', 2018): 0,
('Tech 4', 2019): -24},
'rate': {('Tech 1', 2018): -10, ('Tech 1', 2019): 17,
('Tech 2', 2018): -87, ('Tech 2', 2019): -8, ('Tech 3', 2018): -9,
('Tech 3', 2019): 4, ('Tech 4', 2018): 12, ('Tech 4', 2019): -10},
'net': {('Tech 1', 2018): '-', ('Tech 1', 2019): '+',
('Tech 2', 2018): '-', ('Tech 2', 2019): '-', ('Tech 3', 2018): '-',
('Tech 3', 2019): '+', ('Tech 4', 2018): '+', ('Tech 4', 2019): '-'},
'total': {('Tech 1', 2018): 634, ('Tech 1', 2019): 651,
('Tech 2', 2018): 2389, ('Tech 2', 2019): 2401, ('Tech 3', 2018): 49,
('Tech 3', 2019): 53, ('Tech 4', 2018): 687, ('Tech 4', 2019): 701},
'remaining': {('Tech 1', 2018): 313, ('Tech 1', 2019): 330,
('Tech 2', 2018): 1321, ('Tech 2', 2019): 1313, ('Tech 3', 2018): 38,
('Tech 3', 2019): 42, ('Tech 4', 2018): 145, ('Tech 4', 2019): 135}}))
现在,当按HTML格式渲染时,它似乎足以满足最大列宽的要求,但我想将其设置为更大的范围(如加4),以使它们不正确。最大的价值。
我找到了一种通过遵循this生成HTML后使其工作的方法,但这不是我想要的。它可以工作,但是如果有解决方案,我对寻找解决方案更感兴趣。