Search code examples
pythonpandastype-conversiondata-analysis

How to convert a column in pandas data frame which has special characters in its values and also None as a value?


I have a column named Value in my jeopardy data frame. I am giving the column here

enter image description here

I want to remove the '$' and the ',' symbols and also convert the whole column into a float dtype. I used the following code:

jeopardy['Value'].replace(regex=True, inplace=True, to_replace=r'[$,]', value=r'')
jeopardy['Value'] = jeopardy['Value'].astype(float)

which gave me the following error:

ValueError: could not convert string to float: 'None'

I even tried this

jeopardy['Value'] = pd.to_numeric(jeopardy['Value'],errors='coerce')

which converts the whole column values into Nan.

I want to convert the whole jeopardy['Value'] column into a float dtype by removing the '$' and ','


Solution

  • I think you need espace $ because special regex character and pass to to_numeric:

    jeopardy['Value'] = pd.to_numeric(jeopardy['Value'].replace('[\$,]', '',regex=True), 
                                      errors='coerce')