Screen Link:
https://app.dataquest.io/m/294/guided-project%3A-exploring-ebay-car-sales-data/3/initial-exploration-and-cleaning
My Code:
def clean_price(string):
list_to_replace = ["$","km",","]
for l in list_to_replace:
string = string.replace(l,"")
string = pd.to_numeric(string)
return string
autos[["price","odometer"]].apply(clean_price, axis=1)
What I expected to happen:
I expect the 2 columns to be cleaned and converted to numeric
What actually happened:
ValueErrorTraceback (most recent call last)
pandas/_libs/src/inference.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "150,000km"
During handling of the above exception, another exception occurred:
ValueErrorTraceback (most recent call last)
<ipython-input-61-9769805d5738> in <module>()
6 return string
7
----> 8 autos[["price","odometer"]].apply(clean_price, axis=1)
9
10
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4875 f, axis,
4876 reduce=reduce,
-> 4877 ignore_failures=ignore_failures)
4878 else:
4879 return self._apply_broadcast(f, axis)
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
4971 try:
4972 for i, v in enumerate(series_gen):
-> 4973 results[i] = func(v)
4974 keys.append(v.name)
4975 except Exception as e:
<ipython-input-61-9769805d5738> in clean_price(string)
3 for l in list_to_replace:
4 string = string.replace(l,"")
----> 5 string = pd.to_numeric(string)
6 return string
7
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/tools/numeric.py in to_numeric(arg, errors, downcast)
131 coerce_numeric = False if errors in ('ignore', 'raise') else True
132 values = lib.maybe_convert_numeric(values, set(),
--> 133 coerce_numeric=coerce_numeric)
134
135 except Exception:
pandas/_libs/src/inference.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: ('Unable to parse string "150,000km" at position 1', 'occurred at index 0')
What am I doing wrong? It says unable to parse string “150,000km” at position 1, but I removed the chars in my function?