1

By default pandas adds a bunch of (auto-generated) css classes to the html representation of a DataFrame. It's also possible to attach custom css classes to the DataFrame, however only for the entire frame. I would like to gain some finer control over the styling of columns/cells. Due to some constraints the manipulation via attributes is not a feasible option (showcased in the docs).

Is it possible to add custom css classes to certain columns?

1 Answers1

1

Something like this works, with "tag" and "notes" being columns:

df_data = pd.DataFrame(...)

df_data_s = df_data.set_table_styles({
'tag'   : [dict(selector='td', props=[('max-width', '150px'),('word-wrap', 'break-word')])],
'notes' : [dict(selector='td', props=[('max-width', '150px'),('word-wrap', 'break-word')])],
overwrite=False)

html = df_data_s.render()

From: https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Table-styles

Mario
  • 11
  • 1