用python進行數(shù)據(jù)分析時,查看數(shù)據(jù),經(jīng)常發(fā)生數(shù)據(jù)被自動顯示成科學記數(shù)法的模式,或者多行多列數(shù)據(jù)只顯示前后幾行幾列,中間都是省略號的情形。
numpy
import numpy as npnp.set_printoptions(suppress=True, threshold=np.nan)
suppress=True 取消科學記數(shù)法
threshold=np.nan 完整輸出(沒有省略號)
pandas
display.[max_categories, max_columns, max_colwidth, max_info_columns, max_info_rows, max_rows, max_seq_items, memory_usage, multi_sparse, notebook_repr_html, pprint_nest_depth, precision, show_dimensions]
詳細介紹文檔:pd.set_option
可以在pd.set_option設(shè)置display.float_format參數(shù)來以政策小數(shù)顯示,比如下面設(shè)置顯示到小數(shù)點后3位
pd.set_option('display.float_format', lambda x: '%.3f' % x)
set_option中還有其它一些控制設(shè)置,包括默認顯示列數(shù),行數(shù)等等
pd.set_option('display.max_columns',5, 'display.max_rows', 100)
import pandas as pdpd.set_option('display.max_columns', 10000, 'display.max_rows', 10000)
display.max_columns 顯示最大列數(shù)
display.max_rows 顯示最大行數(shù)
1、pd.set_option(‘expand_frame_repr’, False)
True就是可以換行顯示。設(shè)置成False的時候不允許換行
2、pd.set_option(‘display.max_rows’, 10)
pd.set_option(‘display.max_columns’, 10)
顯示的最大行數(shù)和列數(shù),如果超額就顯示省略號,這個指的是多少個dataFrame的列。如果比較多又不允許換行,就會顯得很亂。
3、pd.set_option(‘precision’, 5)
顯示小數(shù)點后的位數(shù)
4、pd.set_option(‘large_repr’, A)
truncate表示截斷,info表示查看信息,一般選truncate
5、pd.set_option(‘max_colwidth’, 5)
列長度
6、pd.set_option(‘chop_threshold’, 0.5)
絕對值小于0.5的顯示0.0
7、pd.set_option(‘colheader_justify’, ‘left’)
顯示居中還是左邊,
8、pd.set_option(‘display.width’, 200)
橫向最多顯示多少個字符, 一般80不適合橫向的屏幕,平時多用200.
np.set_printoptions
np.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None)
參數(shù):
precision 設(shè)置浮點數(shù)的精度 (默認值:8)
threshold 設(shè)置顯示的數(shù)目(超出部分省略號顯示, np.nan是完全輸出,默認值:1000)
edgeitems 設(shè)置顯示前幾個,后幾個 (默認值:3)
suppress 設(shè)置是否科學記數(shù)法顯示 (默認值:False)
示例如下:
import numpy as npnp.set_printoptions(precision=4, threshold=8, edgeitems=4, linewidth=75, suppress=True, nanstr='nan', infstr='inf')print("precision=4, 浮點數(shù)精確小數(shù)點后4位: ", np.array([1.23446789]))print("threshold=8, edgeitems=4, 顯示8個,前4后4: ", np.arange(10))np.set_printoptions(formatter={'all': lambda x :'int:'+str(-x)})print("formatter, 格式化輸出: ", np.arange(5))
輸出如下:
[圖片上傳失敗...(image-15f596-1587702700460)]
注意:precision自動四舍五入
詳細介紹文檔: np.set_printoptions
pd.set_option
pd.set_option(pat, value)