[英]Remove Dollar Sign from Entire Python Pandas Dataframe
I'm looking to remove dollar signs from an entire python pandas dataframe.我希望从整个 python pandas 数据框中删除美元符号。 It's similar to this post:类似于这个帖子:
Remove Entire Character 删除整个字符
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.所以我不确定如何替换美元符号。
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
To add to jezrael's answer.添加到 jezrael 的答案。 add 'r' before the backslash string to avoid pep8 invalid escape sequence warning.在反斜杠字符串之前添加 'r' 以避免 pep8 无效转义序列警告。
dftest[colstocheck] = dftest[colstocheck].replace({r'\$':''}, regex = True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.