Pandas中display.max_rows设置被忽略问题:指定显示100行却仅显示10行如何解决?
解决Pandas
display.max_rows设置被忽略的问题 这个问题其实是Pandas的行显示逻辑在搞鬼——你设置了display.max_rows=100但只看到10行,核心原因是**display.min_rows参数的默认值在起作用**。
为什么会这样?
Pandas的行显示规则是这样的:
- 如果你的DataFrame行数 ≤
display.max_rows:会显示所有行 - 如果行数 >
display.max_rows:会显示display.min_rows指定的行数(默认是10),前后各展示一部分,中间用...省略
你的DataFrame有1460行,明显超过了设置的100行,所以Pandas自动触发了默认的min_rows=10,导致只显示10行。之前的display.height参数已经被弃用,现在官方是用max_rows和min_rows配合来控制显示的。
怎么解决?
你只需要把display.min_rows也设置为100,这样当行数超过max_rows时,就会显示100行而不是默认的10行。
具体代码操作:
import pandas as pd # 先确认当前的参数值(可选) print("当前max_rows:", pd.get_option('display.max_rows')) print("当前min_rows:", pd.get_option('display.min_rows')) # 设置max_rows和min_rows为100 pd.set_option('display.max_rows', 100) pd.set_option('display.min_rows', 100) # 或者用链式写法更简洁 pd.set_option('display.max_rows', 100).set_option('display.min_rows', 100)
这样设置后,你的1460行DataFrame就会显示前后各50行(总共100行),中间用...省略,完全符合你想要的显示100行的需求。
如果之后你想恢复默认设置,只需要执行:
pd.reset_option('display.max_rows') pd.reset_option('display.min_rows')
内容的提问来源于stack exchange,提问作者Marwan_Tamer
相关产品推荐
相关产品推荐

