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

猜你喜欢

转载自blog.csdn.net/weixin_40309268/article/details/83579381