2017-04-01 79 views
1

我需要格式化一系列浮動顯示逗號劃界每3個數字:使用字符串格式()格式化一個數

1234567.89123 

應該成爲

1,234,567.89123 

不使用:

locale.setlocale(locale.LC_ALL, 'en_US.utf8') 

我可以在浮動轉換爲字符串,然後插入逗號每三位數字從左小數點開始... 是否有更好的辦法嗎?

+0

檢查[這個答案](http://stackoverflow.com/a/10742904/6153990臨時設置選項)。 –

+0

的可能的複製[如何打印用逗號作爲千位分隔符是多少?(http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators) – Farini

回答

2

Refer to this for more information on how to format

s = pd.Series([12345678.9876] * 3) 

s.apply('{:,}'.format) 

0 12,345,678.9876 
1 12,345,678.9876 
2 12,345,678.9876 
dtype: object 

pandas還允許您使用pd.option_context

with pd.option_context('display.float_format', '{:,}'.format): 
    print(s) 

0 12,345,678.9876 
1 12,345,678.9876 
2 12,345,678.9876 
dtype: float64