1. 程式人生 > >python3.x使用numpy,pandas時如何取消科學計數法,顯示完整輸出(無省略號)

python3.x使用numpy,pandas時如何取消科學計數法,顯示完整輸出(無省略號)

部落格1031

用python進行資料分析時,檢視資料,經常發生資料被自動顯示成科學記數法的模式,或者多行多列資料只顯示前後幾行幾列,中間都是省略號的情形。
彙總了下解決辦法,記錄:
環境如下:
python version == 3.6
numpy version == 1.11.3
pandas version == 0.19.2

numpy

import numpy as np
np.set_printoptions(suppress=True, threshold=np.nan)

suppress=True 取消科學記數法
threshold=np.nan 完整輸出(沒有省略號)

pandas

import pandas as pd
pd.set_option('display.max_columns', 10000, 'display.max_rows', 10000)

display.max_columns 顯示最大列數
display.max_rows 顯示最大行數

順便記下文件裡這兩個函式的引數:

np.set_printoptions

np.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None)
引數:
precision 設定浮點數的精度 (預設值:8)
threshold 設定顯示的數目(超出部分省略號顯示, np.nan是完全輸出,預設值:1000)
edgeitems 設定顯示前幾個,後幾個 (預設值:3)
suppress 設定是否科學記數法顯示 (預設值:False)
示例如下:

import numpy as np
np.set_printoptions(precision=4, threshold=8, edgeitems=4, linewidth=75, suppress=True, nanstr='nan', infstr='inf')
print("precision=4, 浮點數精確小數點後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))

輸出如下:
在這裡插入圖片描述
注意:precision自動四捨五入
詳細介紹文件: np.set_printoptions

pd.set_option

pd.set_option(pat, value)
引數:
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