Skip to content
Advertisement

How to adjust row height with pandas Styler

I have a styled pandas DataFrame that I want to export to HTML with smaller rows than default. I don’t know much about CSS so I haven’t found a way to make it work so far. Can it be achieved and if so, how?

import pandas as pd

df = pd.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]})
styler = df.style
...  # Here I adjust a few styling options
html = styler.to_html()

Advertisement

Answer

Set the styles for row and data cell line height and reset the padding

styler.set_table_styles([
    {"selector": "tr", "props": "line-height: 12px;"},
    {"selector": "td,th", "props": "line-height: inherit; padding: 0;"}
])
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement