Home  >  Q&A  >  body text

python - 用pandas展示数据输出时列名不能对齐?

用pandas的sort_values对数据进行排序后输出,发现列名一直没法对齐,这是为什么?

阿神阿神2741 days ago839

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:09:44

    The reason is because the print operation is for service under the console line, and this display can basically only meet basic needs.

    Use jupyter的话,可以直接去掉print, the table will be parsed into an HTML table, and this problem will disappear.

    However, a code cell can only output one result by default. If it is output at the same time

    df1
    df2

    Only df2 will be displayed, as shown below:

    A simple strategy is to use Ipythonmodules.

    from IPython.display import display
    
    display(df1)
    display(df2)

    reply
    0
  • Cancelreply