如何使用Pandas打印CSV文件的全部列?
解决Pandas无法打印全部列的问题
核心问题分析
- Pandas默认会截断过多的列/行,避免输出内容过长
- 你使用
.to_html()时未加括号调用,导致输出的是方法对象而非实际HTML字符串(也就是你看到的<bound method...开头内容)
具体解决方案
1. 调整Pandas全局显示设置
在脚本开头或者函数内添加以下代码,强制显示所有列、行及完整的列内容:
import pandas as pd # 显示所有列 pd.set_option('display.max_columns', None) # 显示所有行(如果数据行数过多,可设为具体数值比如100) pd.set_option('display.max_rows', None) # 防止列内文本被截断 pd.set_option('display.max_colwidth', None) # 优化整体显示宽度,避免列换行 pd.set_option('display.width', 1000)
2. 修正.to_html()和.to_string()的调用方式
你之前的错误是遗漏了方法调用的括号,正确写法如下:
# 以纯文本格式打印完整数据 print(result.to_string()) # 以HTML格式打印(如果需要导出或查看结构化内容) print(result.to_html())
3. 适配你的代码修改示例
把显示设置和正确打印逻辑加入你的函数:
import pandas as pd import matplotlib.pyplot as plt # 全局显示设置放在脚本开头 pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) pd.set_option('display.max_colwidth', None) pd.set_option('display.width', 1000) def region_check(region, startdate, enddate, house_type): df1 = df.loc[:, startdate:enddate] df2 = df.loc[:, 'Region Code':'Rooms'] prop_filter = df["Property_Type"] == house_type result = pd.concat([df2, df1], axis=1, join='inner').where(df2["Region"] == region).where(prop_filter) result = pd.DataFrame(result) result.dropna(inplace=True) # 正确打印完整数据内容 print(result.to_string()) # 或者直接print(result),因为已经设置了全局显示选项 ave = df1.mean() ave.plot() plt.show() return result
额外提示
如果数据量极大,打印全部内容会导致输出混乱,此时可以:
- 用
result.head(10)只打印前10行预览 - 用
result.info()查看数据结构和列信息 - 用
result.columns.tolist()单独列出所有列名,确认日期列是否都被正确包含
内容的提问来源于stack exchange,提问作者Jack Carruthers
相关产品推荐
相关产品推荐

