编辑
2026-04-01
undefined
00

目录

行列设置
折叠设置
float型数据
阈值设置
绘图后端设置
临时设置

行列设置

import pandas as pd print(pd.get_option("display.max_columns")) # 显示列数 print(pd.get_option("display.max_rows")) # 显示行数 pd.set_option("display.max_columns", 1000) # 如果设置成 None 则显示所有 pd.set_option("display.max_rows", 1000) pd.reset_option("display.max_columns") # 重置为默认值 pd.reset_option("display.max_rows") print(pd.get_option("display.max_colwidth")) # 默认列宽50字符 pd.set_option("colheader_justify", "left") # 列字段左对齐 pd.set_option("display.width", 1000)# 设置打印宽度为1000 pd.reset_option("^display") # 重置所有以 display 开头的设置 pd.reset_option("all") # 重置所有设置

折叠设置

当输出数据宽度,超出设置宽度时,是否要折叠。通常使用False不折叠,True要折叠。

pd.set_option("expand_frame_repr", False) # False不折叠,True要折叠

float型数据

pd.set_option("display.precision", 2) # 设置精度为2位小数 ,是千分位表示 %是百分比表示,还可以使用其他特殊符号来表示,例如¥ pd.set_option("display.float_format", "{:,.2f}%".format)

阈值设置

pd.set_option("display.chop_threshold", 0) # 设置零门槛 pd.set_option("display.chop_threshold", 0.5) # 小于0.5显示为0

绘图后端设置

pandas默认使用matplotlib作为绘图后端,可设置修改为强大的plotly:

df = pd.DataFrame(dict(a=[5, 3, 2], b=[3, 4, 1])) pd.options.plotting.backend = "plotly" # 写法1 fig = df.plot() fig.show() fig = df.plot(backend="plotly") # 写法2 fig.show()

临时设置

print(pd.get_option("display.max_rows")) print(pd.get_option("display.max_columns")) with pd.option_context("display.max_rows", 100, "display.max_columns", 100): print(pd.get_option("display.max_rows")) print(pd.get_option("display.max_columns")) print(pd.get_option("display.max_rows")) print(pd.get_option("display.max_columns"))

本文作者:a

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!