从整个 Python Pandas 数据框中删除美元符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43096522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Dollar Sign from Entire Python Pandas Dataframe
提问by d84_n1nj4
I'm looking to remove dollar signs from an entire python pandas dataframe. It's similar to this post:
我希望从整个 python pandas 数据框中删除美元符号。类似于这个帖子:
However, I'm looking to remove the dollar sign which is not working. I believe it's because regex sees the dollar sign as the end of the string, but I'm not sure what to do about it. Here is what I have created so far:
但是,我希望删除不起作用的美元符号。我相信这是因为正则表达式将美元符号视为字符串的结尾,但我不确定该怎么做。这是我迄今为止创建的内容:
dftest = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6],
'C':['f;','$d:','sda%;sd$'],
'D':['s%','d;','d;p$'],
'E':[5,3,6],
'F':[7,4,3]})
Which gives the output:
这给出了输出:
In [155]: dftest
Out[155]:
A B C D E F
0 1 4 f; s% 5 7
1 2 5 $d: d; 3 4
2 3 6 sda%;sd$ d;p$ 6 3
I then try to remove the dollar signs as follows:
然后我尝试删除美元符号如下:
colstocheck = dftest.columns
dftest[colstocheck] = dftest[colstocheck].replace({'$':''}, regex = True)
That does not remove the dollar signs but this code does remove the percent signs:
这不会删除美元符号,但此代码确实删除了百分号:
dftest[colstocheck] = dftest[colstocheck].replace({'%':''}, regex = True)
So I'm not sure how to replace the dollar signs.
所以我不确定如何替换美元符号。
回答by jezrael
You need escape $
by \
:
您需要$
通过\
以下方式逃脱:
dftest[colstocheck] = dftest[colstocheck].replace({'$':''}, regex = True)
print (dftest)
A B C D E F
0 1 4 f; s% 5 7
1 2 5 d: d; 3 4
2 3 6 sda%;sd d;p 6 3