I’d like to generate a html output of a dataframe with thousand separators in the output.
However pl.Config does not seem to do anything:
import polars as pl
df = pl.DataFrame({'prod':['apple','banana','melon'], 'price':[7788, 1122, 4400]})
with pl.Config(
thousands_separator=" "
):
html = '<html><body><table><tr><th>Product</th><th>Price</th></tr>'
html += ''.join(df.with_columns((pl.lit('<tr><td>')+
pl.col('prod')+
pl.lit('</td><td class="right">')+
pl.col('price').cast(pl.String)+
pl.lit('</td></tr>')
).alias('x')
)
.get_column('x')
.to_list()
)
html += '</table></body></html>'
print(html)
2
Answers
thousands_separator
is only applied to numeric columns.But these formatting settings only come into play when you "print" the frame/series.
You could attempt to format the string using Polars expressions.
Although it may be simpler to just do it outside of Polars using regular Python.
# Output
In addition to the answer provided, you are printing python strings, which Polars does not format for you regardless of type.
Here’s a pure Python solution using
f"{number:,}"
to add a comma as a thousands separator then replacing it with a space. This may not be what you are after, but I figure I’d put it out there.Polars does have a format function, but it does not support Python format specifiers (e.g.,
f"{1000:,.2f}"
)