
在使用 Pandas 进行数据分析时,经常需要打印 DataFrame 或 Series 的内容到控制台查看。当数据量较大时,默认的打印输出可能不太友好,因此 Pandas 提供了一些设置来优化打印输出。
我们这里设定一个在print(df)前输出表格样式的方法
管理打印出来的表格,就不需要每次打印都调整了,一般来说只会进行几个df打印
pandaprint()print(df)
https://blog.csdn.net/qq_14815199/article/details/120946121
import numpy as npimport pandas as pddef pandaprint():# 可以在大数据量下,没有省略号pd.set_option("display.max_columns", None)# 最多显示5列,如果表格有列,用分隔# pd.set_option("display.max_columns", 5)# 0 or 20# 在 repr() 方法中使用 max_rows 和 max_columns 来决定是否使用 to_string() 或 info() 将对象呈现为字符串。# 如果 Python/IPython 在终端中运行,则默认设置为 0,# pandas 将正确自动检测终端的宽度并切换到较小的格式,以防所有列都无法垂直放置。# IPython notebook、IPython qtconsole 或 IDLE 不在终端中运行,# 因此无法进行正确的自动检测,在这种情况下,默认值设置为 20。“无”值意味着无限制。# 可以在大数据量下,没有省略号pd.set_option("display.max_rows", None)# 最多显示10行# pd.set_option("display.max_rows", 10)# 显示两位小数位数pd.set_option("display.float_format",lambda x: "%.2f"%x)# 整个打印表的宽度(不够会叠起来)pd.set_option("display.width", 1000000)# 每一列最大10个字(包括...省略号的个字)pd.set_option("max_colwidth", 10)# 浮点型的数值阈值,DataFrame中所有绝对值小于10的数值,都会以0展示pd.set_option("display.chop_threshold", 10)# 左对齐pd.set_option("display.colheader_justify", "left")# 右对齐# pd.set_option("display.colheader_justify", "right")# 26-04-2022# pd.set_option("display.date_dayfirst", True)# 原始数据样式# pd.set_option("display.date_dayfirst", False)# 2022-04-26pd.set_option("display.date_yearfirst", True)# 原始数据样式# pd.set_option("display.date_yearfirst", False)# 多索引省略# pd.set_option("display.multi_sparse", False)# 多索引全展示pd.set_option("display.multi_sparse", True)# 是否折叠pd.set_option("display.expand_frame_repr", False)# 改善对齐pd.set_option("display.unicode.ambiguous_as_wide", True)# 改善对齐pd.set_option("display.unicode.east_asian_width", True)