1

I'm formatting a data frame (or rather a styler object) to have all elements be of the same length and preceded by a dollar sign. However, in the output "excessive" spaces are removed, i.e. only one space remains between the number and the dollar sign regardless of how many digits the actual number contains. See below for an example.

import pandas as pd

df = pd.DataFrame([[123456, 1234.56], [123, 12345678.9123]])
df.style.format('${:15,.2f}')

Which gives the following output:

enter image description here

If I instead pad the numbers using zeroes

df.style.format('${:015,.2f}')

it gives the following output:

enter image description here

How come the padding is truncated when using spaces, but not when using 0's (or other characters such as '_' or '-')? What can I do to avoid this behavior?

jper
  • 41
  • 2
  • 1
    You can try `pd.options.display.float_format` instead, as suggested here: https://stackoverflow.com/questions/20937538 – costaparas Dec 16 '20 at 06:41
  • Thanks! That gives the expected output when displaying the df. However, there are two caveats to this (which I may not have made clear in the original post). Firstly, I wish to only apply this formatting to a subset of my float columns. Secondly, my goal is to export the df to an html file with the formatting intact, `pd.options.display.float_format` does not seem to work for this. – jper Dec 16 '20 at 12:34

0 Answers0