Im my dataframe I have a column with numbers a format with the comma as separator, like 0,5
and the data type inferred is object. I need to convert them to numbers, but when I try df['ColumnName'] = pd.to_numeric(df['ColumnName'])
I get the following exception:
ValueError Traceback (most recent call last)
pandas/_libs/lib.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "2,55"
I tried looking on google and so, but couldn't find a solution. The to_numeric
method's signature does not allow to specify a separator.
Any ideas on how could I fix the problem?
You can replace ,
with .
:
df['ColumnName'] = pd.to_numeric(df['ColumnName'].str.replace(',', '.'))
On the other note, if you read the data with pd.read_csv
, there's an option decimal=','
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments