My sample dataframe looks like below:
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?