【问题标题】:Display pandas dataframe with larger font in jupyter notebook在 jupyter notebook 中显示具有较大字体的 pandas 数据框
【发布时间】:2019-11-11 12:16:49
【问题描述】:

我有一个小型(5 行,3 列)数据框,我可以通过在 jupyter 单元格中调用 df 来显示它。我想放大(字体大小)此输出以进行演示。有可能吗?

【问题讨论】:

    标签: python pandas dataframe jupyter pandas-styles


    【解决方案1】:

    您可以使用df.style.set_table_styles() 来玩转样式

    这可能会有所帮助:

    heading_properties = [('font-size', '18px')]
    
    cell_properties = [('font-size', '16px')]
    
    dfstyle = [dict(selector="th", props=heading_properties),\
     dict(selector="td", props=cell_properties)]
    
    df.style.set_table_styles(dfstyle)
    

    【讨论】:

    • 效果很好。谢谢! :)
    【解决方案2】:

    要更改笔记本中所有数据框的默认 HTML 样式,并且不必将样式单独应用于每个数据框,您有两种选择:

    1. 单个笔记本使用魔术函数%%html

      放在笔记本的第一个单元格中

      %%html
      <style>
      /* Any CSS style can go in here. */
      .dataframe th {
          font-size: 18px;
      }
      .dataframe td {
          font-size: 16px;
      }
      </style>
      
    2. 对于所有笔记本调整 Jupyter CSS 样式:

      添加 CSS 样式信息

      .dataframe th {
          font-size: 18px;
      }
      .dataframe td {
          font-size: 16px;
      }
      

      到以下文件之一:

      • Jupyter 笔记本

        $HOME/.jupyter/custom/custom.css

      • JupyterLab(取决于主题):

        $HOME/anaconda3/envs/[环境名称]/share/jupyter/lab/themes/@jupyterlab/theme-dark-extension/index.css

        $HOME/anaconda3/envs/[环境名称]/share/jupyter/lab/themes/@jupyterlab/theme-light-extension/index.css

        对于 JupyterLab,不幸的是 custom.css 没有使用,所以我看不到另一种更改 CSS 的方法。

    【讨论】:

      【解决方案3】:

      您可以简单地在一行中使用此代码:

      df.style.set_table_attributes('style="font-size: 17px"')
      

      【讨论】:

        猜你喜欢
        • 2017-06-29
        • 2019-01-28
        • 2018-04-11
        • 2016-04-29
        • 2020-02-22
        • 2018-02-02
        • 1970-01-01
        • 2019-07-28
        • 2019-07-09
        相关资源
        最近更新 更多