serhii.net

In the middle of the desert you can say anything you want

06 Oct 2023

Pandas formatting, suppressing scientific notation and display()-ing stuff

# 2 after comma
pd.set_option("display.precision", 2)
# Suppress scientific notation
pd.options.display.float_format = "{:.0f}".format
# for more natural 100,233.23-like output
pd.options.display.float_format = "{:,.3f}".format

Setting as a context1:

with pd.option_context('display.float_format', lambda x: f'{x:,.3f}'):
    display(df.describe())

Also: I can format a float column (’temporarily’) not just how I always did, but also in a way simpler way2:

# before
ds["percent"].apply(lambda x: f"{x:.2%}")
# after
ds["percent"].apply("{:.2%}".format)

I forgot you can do "string".format(variable)!

Also TIL display() for jupyter-notebooks when it’s not the return value (e.g. if you’re exiting a context, df.describe() alone there would not have shown the description)

Nel mezzo del deserto posso dire tutto quello che voglio.