Is there a way to format the output of a particular column of a panda's data frame (e.g., as currency, with ${:,.2f}
, or percentage, with {:,.2%}
) without changing the data itself?
In this post I see that map
can be used, but it changes the data to strings.
I also see that I can use .style.format
(see here) to print a data frame with some formatting, but it returns a Styler
object.
I would like just to change the default print out of the data frame itself, so that it always print it formatted as specified. (I suppose this means changing __repr__
or __repr_html__
.) I'd assume that there is a simple way of doing this in pandas, but I could not find it.
Any help would be greatly appreciated!
EDIT (for clarification): Suppose I have a data frame df
:
df = pd.DataFrame({"Price": [1234.5, 3456.789], "Increase": [0.01234, 0.23456]})
I want the column Price
to be formatted with "${:,.2f}"
and column Increase
to be formatted with "{:,.2%}"
whenever I print df
in a Jupyter notebook (with print
or just running a cell ending in df
).
I can use
df.style.format({"Price": "${:,.2f}", "Increase": "{:,.2%}"})
but I do not want to type that every time I print df
.
I could also do
df["Price"] = df["Price"].map("${:,.2f}".format)
df["Increase"] = df["Increase"].map("{:,.2%}".format)
which does always print as I want (with print(df)
), but this changes the columns from float64
to object
, so I cannot manipulate the data frame anymore.