Pandas Column/Index Values With Dollar Signs

落花浮王杯 提交于 2021-01-28 04:53:38

问题


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:

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/38150927/pandas-column-index-values-with-dollar-signs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!