【问题标题】:pandas/JupyterLab hiding first half of string between $$pandas/JupyterLab 在 $$ 之间隐藏字符串的前半部分
【发布时间】:2022-01-17 11:13:37
【问题描述】:
            one          two        three
0  $97500_$9500  $9000_$7500          nan
1  $97500_$9500  $9000_$7500         7000
2  $97500_$9500  $9000_$7500         7000
3  $97500_$9500  $9000_$7500         7000
4  $97500_$9500  $9000_$9900  $7500_$7000
5         97500        77500         7000
6          7700         7000         7000
7          9000         7500          nan
8          9000         7500         7000
9          9500         7500         7000

当我在 Jupterlab 中显示这个 pandas 数据框时,它看起来像这样,在列之间的框中隐藏了值:

显然是用两个$ 括起来才导致这种情况发生,但我在文档中的任何地方都找不到。有没有人遇到过这个?该功能的用途是什么?

【问题讨论】:

  • stackoverflow.com/questions/16089089/… 会回答您的问题吗?我通过将jupyterlab dollar sign 放入搜索引擎找到了它。看到那里提到MathJax,我尝试再次搜索jupyterlab mathjax,并找到this documentation
  • 那么暗示JupyterLab中的dataframe输出是用markdown/LaTeX生成的?
  • whoaaaaaa... 是的,这让我大吃一惊。它似乎确实在渲染降价,而且似乎也有问题。我正在使用 jupyterlab '3.0.18' 这会将 pd.DataFrame({'A': ['$\alpha$']}) 呈现为 lpha
  • 哦,哎呀,这只是我愚蠢的未能逃脱反斜杠。无论如何......是的,这是一个与 jupyter 渲染问题交互的熊猫。

标签: python pandas numpy jupyter-notebook jupyter-lab


【解决方案1】:

破解

# Data preparing
>>> import pandas as pd
>>> from numpy import nan
>>> df = pd.DataFrame({'one': {0: '$97500_$9500',1: '$97500_$9500',2: '$97500_$9500',3: '$97500_$9500',4: '$97500_$9500',5: '97500',6: '7700',7: '9000',8: '9000',9: '9500'},'two': {0: '$9000_$7500',1: '$9000_$7500',2: '$9000_$7500',3: '$9000_$7500',4: '$9000_$9900',5: '77500',6: '7000',7: '7500',8: '7500',9: '7500'},'three': {0: nan,1: '7000',2: '7000',3: '7000',4: '$7500_$7000',5: '7000',6: '7000',7: nan,8: '7000',9: '7000'}})

# A hack
>>> df = df.apply(lambda x:x.str.replace("$", "\$"))
>>> df

【讨论】:

    【解决方案2】:

    Pandas 有一个 display option display.html.use_mathjax,默认为 True

    当为 True 时,Jupyter notebook 将使用 MathJax 处理表格内容,呈现由美元符号括起来的数学表达式。

    您可以使用pd.set_option('display.html.use_mathjax', False) 更改此设置。这将禁用 pandas 中的自动 mathjax 样式。

    或者,您可以尝试更改样式。请参阅参考类似情况的此问题:https://github.com/pandas-dev/pandas/issues/40318

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 2015-11-17
      相关资源
      最近更新 更多