0

My sample dataframe looks like below:

Sample Dataset

When you see Column "A" of the dataframe, there are values with "+" or "-" and are of dtype as "Object". I want to remove the unwanted "+" or "-" characters and make it "float"

I tried to do this using regular expression as below:

import re

def strip_character(A):
    r = re.compile(r'[["-"]+$]') # tried only for "-"
    return r.sub('', A)

df.A= df.A.apply(strip_character)

But, I am getting below error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-d0c11fa30083> in <module>
      5     return r.sub('', A)
      6 
----> 7 df.A = df.A.apply(strip_character)

~\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
   4043             else:
   4044                 values = self.astype(object).values
-> 4045                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4046 
   4047         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-6-d0c11fa30083> in strip_character(A)
      3 def strip_character(A):
      4     r = re.compile(r'[["-"]+$]') # \s/'-
----> 5     return r.sub('', A)
      6 
      7 df.A = df.A.apply(strip_character)

TypeError: expected string or bytes-like object

This error might be because, the Column A is of "object" data type.

But, my issue is still there and I am unable to remove the unwanted character appeneded to values of A column.

How to fix this issue?

Rinku
  • 1
  • 4
  • From MattR suggestion this answer deserves your attention in particular: https://stackoverflow.com/a/54302517/11610186 – Grzegorz Skibinski Jan 22 '20 at 18:24
  • @MattR. Thanks for the solution. But, again creating issue. It's removing decimal point too. e.g for value"10.10-", it's becoming "1010" – Rinku Jan 22 '20 at 18:27
  • 1
    worked on this --> DF['result'] = DF['result'].str.replace('\+|a|b|\-|A|B', '') Please close this. Thanks a lot. – Rinku Jan 22 '20 at 18:40

0 Answers0