Skip to content

Commit 71837f1

Browse files
author
Aakanksha Ashok
committed
ENH: Support for column specific col_space argument for frame.to_html() method (pandas-dev#28917)
1 parent 6498bc1 commit 71837f1

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

pandas/io/formats/format.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ def __init__(
535535
self,
536536
frame: "DataFrame",
537537
columns: Optional[Sequence[str]] = None,
538-
col_space: Optional[Union[str, int]] = None,
538+
col_space: Optional[Union[str, int, Dict, List]] = None,
539539
header: Union[bool, Sequence[str]] = True,
540540
index: bool = True,
541541
na_rep: str = "NaN",
@@ -575,6 +575,16 @@ def __init__(
575575
" DataFrame number of columns({dlen})"
576576
).format(flen=len(formatters), dlen=len(frame.columns))
577577
)
578+
579+
if isinstance(col_space, list) and len(col_space) != len(frame.columns):
580+
raise ValueError(
581+
(
582+
"Length of col_space list ({clen}) should match"
583+
" number of DataFrame columns ({dlen})".format(
584+
clen=len(col_space), dlen=len(frame.columns)
585+
)
586+
)
587+
)
578588
self.na_rep = na_rep
579589
self.decimal = decimal
580590
self.col_space = col_space

pandas/io/formats/html.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,31 @@ def __init__(
5454
self.border = border
5555
self.table_id = self.fmt.table_id
5656
self.render_links = self.fmt.render_links
57-
if isinstance(self.fmt.col_space, int):
58-
self.fmt.col_space = "{colspace}px".format(colspace=self.fmt.col_space)
57+
if isinstance(self.fmt.col_space, dict):
58+
self.fmt.col_space = {
59+
column: self._convert_to_px(value)
60+
for column, value in self.fmt.col_space.items()
61+
}
62+
elif isinstance(self.fmt.col_space, list):
63+
self.fmt.col_space = dict(
64+
zip(
65+
self.frame.columns,
66+
[self._convert_to_px(value) for value in self.fmt.col_space],
67+
)
68+
)
69+
elif isinstance(self.fmt.col_space, int) or isinstance(self.fmt.col_space, str):
70+
self.fmt.col_space = {
71+
column: self._convert_to_px(self.fmt.col_space)
72+
for column in self.frame.columns
73+
}
74+
else:
75+
self.fmt.col_space = {}
76+
77+
@staticmethod
78+
def _convert_to_px(value: Union[int, str]) -> str:
79+
if isinstance(value, int):
80+
return f"{value}px"
81+
return value
5982

6083
@property
6184
def show_row_idx_names(self) -> bool:
@@ -121,9 +144,11 @@ def write_th(
121144
-------
122145
A written <th> cell.
123146
"""
124-
if header and self.fmt.col_space is not None:
147+
if header and self.fmt.col_space is not None and self.fmt.col_space.get(s):
125148
tags = tags or ""
126-
tags += 'style="min-width: {colspace};"'.format(colspace=self.fmt.col_space)
149+
tags += 'style="min-width: {colspace};"'.format(
150+
colspace=self.fmt.col_space.get(s)
151+
)
127152

128153
self._write_cell(s, kind="th", indent=indent, tags=tags)
129154

0 commit comments

Comments
 (0)