2016-03-06 137 views
4

我正在尋找對以下問題的答案已經超過4個小時了。大多數頁面指示字符串格式化方法。這不是我想要的。在iPython中設置千位分隔符,而不使用字符串格式

我想在IPython中爲整數和浮點數分隔符設置一個參數。該選項只應該影響數字在我的交互式會話中的顯示方式。我想設置一次參數。我需要爲每個新輸出做一些格式化的所有解決方案都不能滿足我的需求。我做了一些探索性的數據分析,並且不想爲每行代碼打擾數字格式。

該格式應與所有整數和浮點數一起使用,包括那些存儲在numpy數組或熊貓數據框中的格式。

對於那些熟悉Mathematica的人,我指出瞭如何在Mathematica中做到這一點:轉到preferences => appearance => numbers => formatting。在那裏您可以「啓用自動數字格式化」並選擇「數字塊分隔符」。例如:如果我在我的ipython會話中鍵入「600 + 600」,我需要以下輸出:1'200(其中'將是我的千位分隔符)。

我在Spyder和IPython筆記本中使用IPython控制檯。謝謝。

+0

我現在警告你。這可能是不可能的。 – zondo

回答

8

如果您使用str.formatnumpy.set_printoptions你可以設置它在全球一次:

import numpy as np 
import IPython 

frm = get_ipython().display_formatter.formatters['text/plain'] 


def thousands(arg, p, cycle): 
    p.text("{:,}".format(arg).replace(",","'")) 

frm.for_type(int, thousands) 
frm.for_type(float, thousands) 

np.set_printoptions(formatter={'int_kind': lambda x: '{:,}'.format(x).replace(",","'")}) 

np.set_printoptions(formatter={'float_kind': lambda x: '{:,}'.format(x).replace(",","'")}) 

frm = get_ipython().display_formatter.formatters['text/plain'] 
frm.for_type(int, thousands) 
frm.for_type(float, thousands) 

它不包括所有基地,但你可以添加更多的邏輯:

In [2]: arr = np.array([12345,12345]) 

In [3]: arr 
Out[3]: array([12'345, 12'345]) 

In [4]: 123456 
Out[4]: 123'456 

In [5]: 123456.343 
Out[5]: 123'456.343 

您可以將其添加到startup.py腳本確保您設置PYTHONSTARTUP指向文件,以便在啓動ipython時加載該文件:

~$ ipython2 
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
Type "copyright", "credits" or "license" for more information. 

IPython 4.0.1 -- An enhanced Interactive Python. 
?   -> Introduction and overview of IPython's features. 
%quickref -> Quick reference. 
help  -> Python's own help system. 
object? -> Details about 'object', use 'object??' for extra details. 
(.startup.py) 
(imported datetime, os, pprint, re, sys, time,np,pd) 

In [1]: arr = np.array([12345,12345]) 

In [2]: arr 
Out[2]: array([12'345, 12'345]) 

In [3]: 12345 
Out[3]: "12'345" 

大熊貓似乎可以設置display.float_format與set_option

In [22]: pd.set_option("display.float_format",lambda x: "{:,}".format(x).replace(",","'")) 

In [23]: pd.DataFrame([[12345.3,12345.4]]) 
Out[23]: 
     0  1 
0 12'345.3 12'345.4 

基於this answer似乎更高版本的大熊貓,我們需要改變pandas.core.format.IntArrayFormatter

所以完全啓動腳本會是這樣的:

import IPython 

import numpy as np 
import pandas as pd 

# numpy 
np.set_printoptions(formatter={'float_kind': lambda x: '{:,}'.format(x).replace(",", "'"), 
          'int_kind': lambda x: '{:,}'.format(x).replace(",", "'")}) 


# pandas 
class IntFormatter(pd.core.format.GenericArrayFormatter): 
    pd.set_option("display.float_format", lambda x: "{:,}".format(x).replace(",", "'")) 

    def _format_strings(self): 
     formatter = self.formatter or (lambda x: ' {:,}'.format(x).replace(",", "'")) 
     fmt_values = [formatter(x) for x in self.values] 
     return fmt_values 


pd.core.format.IntArrayFormatter = IntFormatter 


# general 
def thousands(arg, p, cycle): 
    p.text("{:,}".format(arg).replace(",","'")) 


frm = get_ipython().display_formatter.formatters['text/plain'] 
frm.for_type(int, thousands) 
frm.for_type(float, thousands) 

這似乎涵蓋了大部分的你想要什麼:

IPython 4.0.1 -- An enhanced Interactive Python. 
?   -> Introduction and overview of IPython's features. 
%quickref -> Quick reference. 
help  -> Python's own help system. 
object? -> Details about 'object', use 'object??' for extra details. 
(.startup.py) 
(imported datetime, os, pprint, re, sys, time,np,pd) 

In [1]: pd.DataFrame([[12345,12345]]) 
Out[1]: 
     0  1 
0 12'345 12'345 

In [2]: pd.DataFrame([[12345,12345.345]]) 
Out[2]: 
     0   1 
0 12'345 12'345.345 

In [3]: np.array([12345,678910]) 
Out[3]: array([12'345, 678'910]) 

In [4]: np.array([12345.321,678910.123]) 
Out[4]: array([12'345.321, 678'910.123]) 


In [5]: 100000 
Out[5]: 100'000 

In [6]: 100000.123 
Out[6]: 100'000.123 

In [7]: 10000000 
Out[7]: 10'000'000 
+0

優秀的答案!這解決了我與numpy和熊貓的問題。對於一般情況,它不是我想要的,因爲它將數字轉換爲字符串。非常感謝! – Steve

+1

@Steve,我不知道有什麼方法可以以自定義格式顯示,而不使用類似上面的東西,我對千位函數進行了更改,使得從輸出中刪除雙引號,但這樣做會更好無論如何,從我這裏得到 –