【问题标题】:Wrapping column names in Python Pandas DataFrame or Jupyter Notebooks在 Python Pandas DataFrame 或 Jupyter Notebooks 中包装列名
【发布时间】:2017-08-22 20:56:07
【问题描述】:

我的数据框中的某些列的标题很长,我希望能够换行。我知道这个功能是内置在 pandas 中的,就像我一样:

pd.DataFrame(np.random.randn(2, 10), 
    columns=['Very Long Column Title ' + str(i) for i in range(10)])

DataFrame with wrapped column names

但是如果我有更少的列,标题将不会换行:

pd.DataFrame(np.random.randn(10, 2), 
    columns=['Very Long Column Title ' + str(i) for i in range(2)])

DataFrame does not wrap column names

我也尝试过手动插入换行符:

import pandas as pd    
pd.DataFrame(np.random.randn(10, 2), 
    columns=['Very Long \n Column Title ' + str(i) for i in range(2)])

但这给出了与上面相同的输出。

我在这个主题上找到了类似的答案:

我正在使用 Jupyter 笔记本,但如果可能,我更喜欢基于 pandas 的解决方案。

【问题讨论】:

    标签: python pandas dataframe formatting jupyter-notebook


    【解决方案1】:

    Jupyter 笔记本从多个来源继承其显示属性。 pandas 中没有限制列标题宽度的属性,因为 pandas 不是导致文本换行的原因,它实际上是呈现的 HTML。

    您可以使用以下方法覆盖默认的 Jupyter Notebook 样式以限制表头的最大宽度:

    from IPython.core.display import HTML
    HTML("<style>.rendered_html th {max-width: 120px;}</style>")
    

    在笔记本顶部运行此代码一次,将 html 表格标题的最大列宽设置为 120 像素。

    【讨论】:

    • 完美!我知道这一定是某个地方的设置,但不知道在哪里。
    【解决方案2】:

    这是一个不涉及更改 IPython 属性的答案:

    df = pd.DataFrame(np.random.randn(10, 2), 
        columns=['Very Long Column Title ' + str(i) for i in range(2)])
    df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
    

    【讨论】:

    • 谢谢!一个原生的 pandas 结果,这是我所希望的。
    • 此解决方案仅适用于 jupyter,不适用于笔记本之外。
    • 谢谢 - 谢谢 - 谢谢。这与其他 pandas.style 功能非常有效。
    • 如果你的列名没有空格,即very_long_column_title_0而不是Very Long Column Title 0,你可能还需要在调用set_table_styles()时将"word-break CSS属性设置为break-all .
    【解决方案3】:

    您可以通过在列标题中插入空格来“破解”正确的行为:

    def colfix(df, L=5): return df.rename(columns=lambda x: ' '.join(x.replace('_', ' ')[i:i+L] for i in range(0,len(x),L)) if df[x].dtype in ['float64','int64'] else x )
    
    colfix(your_df)
    

    查看我对类似问题的回答https://stackoverflow.com/a/45078833/6903458

    【讨论】:

      【解决方案4】:

      或者,您可以使用包textwrap

      import textwrap
      
      cols = ['Very Long Column Title ' + str(i) for i in range(2)]
      
      # Split wide columns, you can then join these with any delimiter you'd like
      cols = [textwrap.wrap(x, width=20) for x in cols]
      
      # print(cols)
      # [['Very Long Column', 'Title 0'], ['Very Long Column', 'Title 1']]
      
      

      【讨论】:

        【解决方案5】:

        @AndreyF 的答案的一个版本,无论您是否使用 Jupyter,它都有效。该例程使用 Pandas 样式器将数据框呈现为 HTML。然后,要查看表格,您必须将 HTML 保存到文件并在浏览器中打开它。请注意,样式器被显式捕获为变量。

        df = pd.DataFrame(np.random.randn(10, 2), 
            columns=['Very Long Column Title ' + str(i) for i in range(2)])
        styler = df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
        with open("/tmp/testStyle.html",'w') as f: f.write(styler.render())
        

        【讨论】:

          猜你喜欢
          • 2021-12-02
          • 2021-09-10
          • 2019-07-21
          • 2019-12-04
          • 2023-03-15
          • 2019-01-28
          • 1970-01-01
          • 2019-04-11
          • 2018-08-21
          相关资源
          最近更新 更多