Skip to content Skip to sidebar Skip to footer

Converting A Pandas Dataframe Column Containing Negative Strings Into Float

I have a pandas Dataframe df that contains negative strings and i would like to convert them to float: NY_resitor1 NY_resitor2 SF_type SF_resitor2 45 '-36

Solution 1:

I think this might be a case of having a strange unicode version of "-" somewhere in your string data. For example, this should work:

>>>import pandas as pd>>>ser = pd.Series(['-36', '36'])>>>ser.astype(float)
0   -36
1    36
dtype: float64

But this doesn't, because I've replaced the standard minus sign with a U+2212 minus sign:

>>>ser2 = pd.Series(['−32', '36'])>>>ser2.astype(float)...
ValueError: could not convert string to float: '−32'

you could address this by specifically getting rid of the offending characters, using str.replace():

>>>ser2.str.replace('−', '-').astype(float)
0   -32
1    36
dtype: float64

If that's not the issue, then I don't know what is!


Edit: another possibility is that your strings could have quotes within them. e.g.

>>>ser3 = pd.Series(['"-36"', '"36"'])>>>ser3.astype(float)...
ValueError: could not convert string to float: '"-36"'

In this case, you need to strip these out first:

>>>ser3.str.replace('"', '').astype(float)
0   -36
1    36
dtype: float64

Post a Comment for "Converting A Pandas Dataframe Column Containing Negative Strings Into Float"