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

如何解决打印含表情符号的Pandas DataFrame时的列对齐问题?

Pandas DataFrame含表情符号时列标题对齐问题

打印包含表情符号的Pandas DataFrame时,列标题的对齐问题会随列数增加而愈发严重;但当DataFrame不含表情符号时,该问题不会出现。以下是问题复现代码及效果:

含表情符号的情况

import pandas as pd

pd.set_option('display.max_rows',    1000)
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width',       1000)

example = {'normal_col'  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
           'text_col'    : ['hello world'] * 10,
           'emoji_col_A' : ['🟩 hello world'] * 10,
           'emoji_col_B' : ['🟥 hello world'] * 10,
           'emoji_col_C' : ['🟧 hello world'] * 10,
           'emoji_col_D' : ['🟨 hello world'] * 10}

df = pd.DataFrame(example)

print(df)

输出:

normal_col     text_col    emoji_col_A    emoji_col_B    emoji_col_C    emoji_col_D
0           1  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
1           2  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
2           3  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
3           4  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
4           5  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
5           6  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
6           7  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
7           8  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
8           9  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world
9          10  hello world  🟩 hello world  🟥 hello world  🟧 hello world  🟨 hello world

不含表情符号的情况

import pandas as pd

pd.set_option('display.max_rows',    1000)
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width',       1000)

example = {'normal_col'  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
           'text_col'    : ['hello world'] * 10,
           'emoji_col_A' : ['hello world'] * 10,
           'emoji_col_B' : ['hello world'] * 10,
           'emoji_col_C' : ['hello world'] * 10,
           'emoji_col_D' : ['hello world'] * 10}

df = pd.DataFrame(example)

print(df)

输出:

normal_col     text_col  emoji_col_A  emoji_col_B  emoji_col_C  emoji_col_D
0           1  hello world  hello world  hello world  hello world  hello world
1           2  hello world  hello world  hello world  hello world  hello world
2           3  hello world  hello world  hello world  hello world  hello world
3           4  hello world  hello world  hello world  hello world  hello world
4           5  hello world  hello world  hello world  hello world  hello world
5           6  hello world  hello world  hello world  hello world  hello world
6           7  hello world  hello world  hello world  hello world  hello world
7           8  hello world  hello world  hello world  hello world  hello world
8           9  hello world  hello world  hello world  hello world  hello world
9          10  hello world  hello world  hello world  hello world  hello world
解决方案

方法1:启用Pandas宽字符宽度识别

Pandas默认对表情符号这类宽字符的宽度计算存在偏差,添加以下配置可让Pandas正确识别宽字符宽度,解决对齐问题:

import pandas as pd

# 启用宽字符宽度计算
pd.set_option('display.unicode.east_asian_width', True)

pd.set_option('display.max_rows',    1000)
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width',       1000)

example = {'normal_col'  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
           'text_col'    : ['hello world'] * 10,
           'emoji_col_A' : ['🟩 hello world'] * 10,
           'emoji_col_B' : ['🟥 hello world'] * 10,
           'emoji_col_C' : ['🟧 hello world'] * 10,
           'emoji_col_D' : ['🟨 hello world'] * 10}

df = pd.DataFrame(example)

print(df)

方法2:使用tabulate库格式化输出

若方法1效果不理想,可借助第三方库tabulate实现精准排版,它对宽字符的支持更完善:

  1. 安装依赖库:
pip install tabulate
  1. 格式化输出代码:
import pandas as pd
from tabulate import tabulate

pd.set_option('display.max_rows',    1000)
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width',       1000)

example = {'normal_col'  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
           'text_col'    : ['hello world'] * 10,
           'emoji_col_A' : ['🟩 hello world'] * 10,
           'emoji_col_B' : ['🟥 hello world'] * 10,
           'emoji_col_C' : ['🟧 hello world'] * 10,
           'emoji_col_D' : ['🟨 hello world'] * 10}

df = pd.DataFrame(example)

# 用tabulate格式化,保持和Pandas默认风格一致
print(tabulate(df, headers='keys', tablefmt='plain', showindex=True))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 16:34:56