0

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.

finotti
  • 1
  • 1
  • I think you missed the first part of the [first answer](/a/20937592/15497888) which uses `pd.options.display.float_format = '${:,.2f}'.format` that does not alter data at all. Or the [second answer](/a/23922119/15497888) which uses formatters for a specific column only with `.to_string(formatters={'cost':'${:,.2f}'.format})` – Henry Ecker Sep 05 '22 at 02:08
  • What do you mean change the default print of the DataFrame itself? If you're only looking to select specific columns to format how do you specify which columns get formatted this way, if you're not looking to change all float columns uniformly like in Q & A you linked? – Henry Ecker Sep 05 '22 at 02:14
  • I do not want to change the default printing of floats, as I want to apply formatting to specific columns. By the "default print out of the data frame", I mean if I just print the data frame itself, like `print(df)` (or having `df` as last line of a Jupyter notebook cell), it will print the formatted version, according to some specified format (for specific column(s)). I would prefer not to use formatting commands every time I print the data frame to obtain the formatted version. – finotti Sep 05 '22 at 02:26
  • Any particular reason you need to overload/monkey patch a pandas DataFrame instead just making a `print_df` function that calls `print(df.to_string(...))`? – Henry Ecker Sep 05 '22 at 03:03
  • Just "ease of use". I assumed there was a good and proper way of doing this, as it seems a very natural feature. I can live with `to_string` if there is no good/proper solution. Thanks for your help! – finotti Sep 05 '22 at 10:28

1 Answers1

0

It is a natural feature, but pandas cannot guess what your format is, and each time you create a styler it has to be informed of such decisions, since it is a separate object. It does not dynamically update if you change your DataFrame.

The best you can do is to create a generic print via styler.

def p(df):
    styler = dy.style
    styler.format({"Price": "${:,.2f}", "Increase": "{:,.2%}"})
    return styler

df = DataFrame([[1,2],[3,4]], columns=["Price", "Increase"])
p(df)
Attack68
  • 4,437
  • 1
  • 20
  • 40