python中的美元符号_从整个Python Pandas Dataframe删除美元符号

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]})

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值