1

I am using formatters to display a column of a Pandas DataFrame in a certain way:

import pandas
df = pandas.DataFrame({"id": [0, 10, 288, 1], "value": [38.8, 88.3, 15, 19.8], "percent": [0.55, 0.05, 0.008, 0.12]})
print(df.to_string(formatters={"percent": "{:}".format}))

which outputs as:

    id  value percent
0    0   38.8    0.55
1   10   88.3    0.05
2  288   15.0   0.008
3    1   19.8    0.12

As you can see, there is only one space between the "values" column and the "percent" column, whereas there should be two spaces (as between "id" and "values"). This is a nitty gritty detail, but how to have the two spaces back?

Pablo C
  • 4,661
  • 2
  • 8
  • 24
Neraste
  • 485
  • 4
  • 15

1 Answers1

0
import pandas
df = pandas.DataFrame({"id": [0, 10, 288, 1], "value": [38.8, 88.3, 15, 19.8], "percent": [0.55, 0.05, 0.008, 0.12]})
print(df.to_string(formatters={"id":"{:4}".format, "value": "{:6}".format,"percent": "{:8}".format}))

Output:

    id  value  percent
0    0   38.8     0.55
1   10   88.3     0.05
2  288   15.0    0.008
3    1   19.8     0.12
len
  • 749
  • 1
  • 8
  • 23