从整个Python Pandas Dataframe删除美元符号 [英] Remove Dollar Sign from Entire Python Pandas Dataframe
问题描述
我正在寻找从整个python熊猫数据框中删除美元符号的方法.类似于这篇文章:
I'm looking to remove dollar signs from an entire python pandas dataframe. It's similar to this post:
但是,我希望删除不起作用的美元符号.我相信这是因为正则表达式将美元符号视为字符串的结尾,但是我不确定该怎么做.到目前为止,这是我创建的:
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]})
哪个给出输出:
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.
推荐答案
您需要通过\
进行转义$
:
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
这篇关于从整个Python Pandas Dataframe删除美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!