Problem with style.format() and a decimal column

Viewed 3955

I am trying to apply a style.format function to a data frame in order to adjust the number of decimal places, since the round() function does not apply. (I don't understand why.)

The data in the df are of the format 123.0, 432.0, and 543.0, but I need them to have two decimal places (e.g., 123.00).

I have verified with the function df.dtypes that the column is of type float.

I tried to apply the following:

import pandas.io.formats.style
import pandas as pd

df['DD'] = df[['Quantity']].style.format("{:.2%}")
df

But the following appears in the DD field:

<pandas.io.formats.style.Styler object at 0x7f...

capture data return

The round() function does not work on the column either.

What can be done?

1 Answers

The .style gives back a Styler object (reference). The Styler object can be formatted in different ways. This has not happened in your DD column; you are only halfway through.

You can format the dataframe with

df.style.format({'Quantity': "{:.2%}"})

as explained in this SO post.

Related