Home > Back-end >  pandas/JupyterLab hiding first half of string between $$
pandas/JupyterLab hiding first half of string between $$

Time:12-14

            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

When I display this pandas dataframe in Jupterlab, it appears like this, with hidden values in boxes between the columns:

enter image description here

It's obviously the bracketing with the two $ that's making this happen, but I can't find this anywhere in the documentation. Has anyone run into this before? What's the purpose of the functionality?

CodePudding user response:

Hack

# 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

enter image description here

CodePudding user response:

Pandas has a display option display.html.use_mathjax which is True by default:

When True, Jupyter notebook will process table contents using MathJax, rendering mathematical expressions enclosed by the dollar symbol.

You can change this with pd.set_option('display.html.use_mathjax', False). This would disable automatic mathjax styling in pandas.

Alternatively, you could try to change the styling. See this issue referencing a similar situation: https://github.com/pandas-dev/pandas/issues/40318

  • Related