You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何打印拼接后的Pandas DataFrame时部分列换行显示?

问题原因及解决办法

原因

你遇到的列换行问题,根源是pandas的print输出受默认显示宽度限制。虽然你设置了display.max_rows和display.max_columns为None,但没调整display.width参数——当拼接后的DataFrame总宽度超过这个默认阈值时,pandas会自动把放不下的列换行到下方,这是控制台输出的格式化逻辑导致的。

解决办法

有两种实用的处理方式:

方式一:修改pandas显示宽度限制

在代码开头添加一行,把显示宽度设得足够大(比如10000,远大于10列数据的总宽度):

pd.set_option('display.width', 10000)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

pdList = [df41,df42,df43,df44,df45,df46,df47,df48,df49,df410]
df4 = pd.concat(pdList, axis=1, ignore_index = True)
df4 = df4.set_index(0)        
with open('data.dat', mode='w') as file_object: 
    print(df4, file = file_object)

方式二:用pandas专门的文件输出方法(更推荐)

print是把控制台的可视化输出写入文件,本身不是为数据持久化设计的。改用to_csv方法直接输出,完全不会有换行问题,还能自定义分隔符:

pdList = [df41,df42,df43,df44,df45,df46,df47,df48,df49,df410]
df4 = pd.concat(pdList, axis=1, ignore_index = True)
df4 = df4.set_index(0)        
# 用\t作为分隔符,也可换成空格或其他符号
df4.to_csv('data.dat', sep='\t', index=True)

内容的提问来源于stack exchange,提问作者Wille Bell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 10:10:40