Search code examples
pandasdataframejupyter-notebookpandas-styles

df.style.format() having no effect on dataframe


I'm using Jupyter Notebook and I have a DataFrame with df.shape = (18,7). All columns are of float64 dtype :

def currency_format(x):
        return "${:,.0f}".format(x/1000000)
    dfResults.style.format(currency_format)
    dfResults

The formater never fires. I tried subsets, no subsets Not sure what I am missing. dfResults just shows all values unformatted.

I can use pd.options to globally set the format, and it works. Just trying to learn selective formatting.


Solution

  • dfResults.style.format(currency_format) has indeed no effect on your original dfResults simply because it returns a complete different object. Not to mention that it is a Styler (not a DataFrame). Also, by switching to the former, you won't have access to the latter's API anymore. Another point, from what I understand through your code, you're running two expressions in the same Jupyter cell and since, by default, only the last one is displayed, you're seeing your original dataframe and thus, with it's default float formatting representation.

    ---------------------------------------- 
    |def currency_format(x):                |
    |    return "${:,.0f}".format(x/1000000)|
    |                                       | 
    |dfResults.style.format(currency_format)|
    |dfResults                              | # << only this one is displayed
    ----------------------------------------
    

    So if you're adjusting the format only for visual purposes, just remove dfResults in the end of the cell or store the styler in a separate variable and display it. Otherwise, if you still need to benefit from the DataFrame's API, setting a custom float format is your only option (which you already did apparently).

    pd.set_option("display.float_format", lambda x: "${:,.0f}".format(x/1e6))
    

    You may also be interested by this Q/A : Pandas : Set format for a single DataFrame.