'ValueError: could not convert string to float: '9.8-10.0 '
CODE:-
I'm trying to convert the values of ARAI_Certified_Mileage column to float but it is throwing an error that string values like '9.8-10.0' can't be converted to float. So I'm thinking of removing 10.0 and converting 9.8 to float, but I'm not getting how to do it.
import os
import pandas as pd
df = pd.read_csv('cars_engage_2022.csv')
df["ARAI_Certified_Mileage"] = df["ARAI_Certified_Mileage"].str.replace(r'.* ([\d,]+)+$', r'\1',regex=True).str.replace('km/litre', '',regex=True).astype('float')
print(df.head())
ERROR:-
Traceback (most recent call last):
File "c:\Users\shweta\OneDrive\Desktop\DEMO\demo\src\carsengage2022\preprocessing\roughKMC.py", line 9, in <module>
df["ARAI_Certified_Mileage"] = df["ARAI_Certified_Mileage"].str.replace(r'.* ([\d,]+)+$', r'\1',regex=True).str.replace('km/litre', '',regex=True).astype('float')
File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\generic.py", line 5912, in astype
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\internals\managers.py", line 419, in astype
return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\internals\managers.py", line 304, in apply
applied = getattr(b, f)(**kwargs)
File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\internals\blocks.py", line 580, in astype
new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\dtypes\cast.py", line 1292, in astype_array_safe
new_values = astype_array(values, dtype, copy=copy)
File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\dtypes\cast.py", line 1237, in astype_array
values = astype_nansafe(values, dtype, copy=copy)
File "C:\Users\shweta\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\dtypes\cast.py", line 1181, in astype_nansafe
return arr.astype(dtype, copy=True)
ValueError: could not convert string to float: '9.8-10.0 '
values of ARAI_Certified_Mileage column:-
ARAI_Certified_Mileage
22.4-21.9 km/litre
9.8-10.0 km/litre
20.6-22.8 km/litre
and so on...
Solution 1:[1]
Can you try this for every mileage item.
inp = ["22.4-21.9", "9.8-10.0 ", "20.6-22.8"]
out = list((float((x.split('-', 1)[0])) for x in inp))
print(out)
# [22.4, 9.8, 20.6]
# if you want the second part of every item to be converted to float, then use below code
out2 = list((float((x.split('-', 1)[1])) for x in inp))
print(out2)
# [21.9, 10.0, 22.8]
What this does is splits every item at the '-', and takes the first part and converts it to float.
Items like this '9.8-10.0 ' cannot directly be converted to a float as they consist of a - in them. So you need to decide whether you want the first part (before -) to be converted to float or the second part(after the -).
Solution 2:[2]
It's about the type of element it refers to. For example, if it's an input, you want to use HTMLInputElement. For all types you may want to check them here.
And for the template, you need to provide a template context, which means what variables are accessible in the template. For more detailed explanation you may want to refer here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | iR0ckY |
| Solution 2 | akkonrad |
