Search code examples
python-3.xpandasipythonjupyter-notebookdollar-sign

Pandas Column/Index Values With Dollar Signs


Given the following data frame:

import pandas as pd
df=pd.DataFrame({'A':['$0-$20','$20+']})
df
    A
0   0−20
1   $20+

How can I get the first value (0-20) to display with the dollar signs, as I originally specified? It actually displays the zero in a strange font and I'm not sure why it works for "$20+" but not "$0-"$20".

I've tried:

df=pd.DataFrame({'A':[str('$0-$20'),'$20+']})

...but no dice.

Here's specifically what I get:

enter image description here

I'm using the Jupyter notebook (Anaconda 3, Python 3.5) Thanks in advance!


Solution

  • In Jupyter, the $0-$ gets interpreted as tagged latex. It looks like you are running in iPython and something similar is probably happening.

    enter image description here

    Notice the odd font on the 0 - and the bar after the -. I know this bar is an artifact of a mathjax/chrome bug, which is what tipped me off.

    You're dataframe is intact. It's displaying it that's an issue.

    Doing something like this should help:

    import pandas as pd
    df=pd.DataFrame({'A':['$0-$20','$20+']})
    print df.__repr__()
    
            A
    0  $0-$20
    1    $20+
    

    The __repr__ method embodies how an object is to be displayed and returns a string. Jupyter-notebook calls _repr_html_ instead and passes it through an HTML handler.