Python Pandas : how to format a float with space between thousands, millions, etc

时间:2018-07-25 12:19:27

标签: python pandas format

With :

pd.options.display.float_format = '{:,}'.format

It displays for number x = 1234567.888 :

1,234,567.888

What is the appropriate format to show 0 decimals and add a space between thousands, millions, billions, and so on ? Like this

1 234 568

1 个答案:

答案 0 :(得分:1)

要使用python寻找此操作,请按以下步骤操作:

from math import trunc
some_float = 1234569.02

print ('{:,}'.format(trunc(some_float)).replace(',', ' '))

您可以了解有关trunc()here的更多信息。

如@Jon Clements所指出的,您还可以使用'{:,。0f}'进行格式化。不需要导入数学库。