INNER CODE UNIT · Python
_compute_column_widths
aeturrell/skimpy · src/skimpy/__init__.py:198
def _compute_column_widths(df: pd.DataFrame) -> list[int]:
"""Compute appropriate max_width for each column based on content.
Examines header length and the longest value in each column, then
clamps to [MIN_COL_WIDTH, MAX_COL_WIDTH].
Args:
df (pd.DataFrame): The dataframe whose columns to measure.
Returns:
list[int]: A max_width value for each column.
"""
widths = []
for col in df.columns:
header_width = len(str(col))
content_width = max((len(str(v)) for v in df[col]), default=0)
ideal = max(header_width, content_width)
clamped = max(MIN_COL_WIDTH, min(MAX_COL_WIDTH, ideal))