Issue
I have this in a Jupyter Notebook / learning Pandas.
df.shape = (18,7) All are float64
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.
Thanks in advance
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.
Answered By - Timeless
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.