I have the following data frame:
Bestand = pd.DataFrame({'ISIN': ['NaN', 'IE00B4X9L533', 'IE00BF4RFH31'], 'Marktwert': ['217.803,37', '47.755,10', '15.353,32']})
In order to calculate with the second column named “Marktwert”, I have to convert the string as a float, the sting has German format, that means the decimal point is a comma and the thousands separator is a dot. The number 217.803,37 has the datatype object.
If I try to convert using the code
Bestand['Marktwert'] = pd.to_numeric(Bestand['Marktwert'], downcast="float")
I get the error
ValueError: Unable to parse string "217.803,37" at position 0
Can anyone help me out pls?
Advertisement
Answer
In case you’re reading the data from a file, many of Pandas’ file readers like pandas.read_csv()
have decimal
and thousands
arguments for exactly this case.