Pandas 显示所有列及格式化输出

本文介绍如何使用 Pandas 显示 DataFrame 的所有列,并提供格式化输出的代码示例,解决列名不对齐问题。

显示所有列

# 显示所有列
pd.set_option('display.max_columns', None)

# 设置宽度
pd.set_option('display.width', 1000)

# 解决数据输出时列名不对齐的问题
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)

print(df1)
print(df2)

代码说明:

  • pd.set_option('display.max_columns', None):设置显示所有列。
  • pd.set_option('display.width', 1000):设置输出宽度为 1000 个字符。
  • pd.set_option('display.unicode.ambiguous_as_wide', True)pd.set_option('display.unicode.east_asian_width', True):解决数据输出时列名不对齐问题,确保中文等宽字符的正确显示。

示例:

import pandas as pd

df1 = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]})  # 创建示例 DataFrame

# 使用上述代码设置显示选项
# 输出结果将显示所有列,并进行格式化
print(df1)

注意:

  • df1df2 是您需要显示的 DataFrame 的名称。
  • 您可以根据需要调整 display.width 的值。

通过以上代码,您可以轻松地显示所有列,并获得格式化后的输出结果,提高数据展示的清晰度和可读性。

Pandas 显示所有列及格式化输出

原文地址: https://www.cveoy.top/t/topic/ehL2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录