如何解决打印含表情符号的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实现精准排版,它对宽字符的支持更完善:
- 安装依赖库:
pip install tabulate
- 格式化输出代码:
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
相关产品推荐
相关产品推荐

