So I am coding along with Complete A.I. & Machine learning, data science bootcamp 2024. On video 55 selecting and viewing data with pandas part 2 of section 6. I try to run the code:
car_sales["Price"] = car_sales["Price"].str.replace('[$,.]', '').astype(int)
I run it and get the following error which I cannot seem to find any solution or fix for:
<>:1: SyntaxWarning: invalid escape sequence '$'
<>:1: SyntaxWarning: invalid escape sequence '$'
C:\Users\sweet\AppData\Local\Temp\ipykernel_16004\2312081839.py:1: SyntaxWarning: invalid escape sequence '$'
car_sales["Price"] = car_sales["Price"].str.replace('[$,.]', '').astype(int)
ValueError Traceback (most recent call last)
Cell In[170], line 1
----> 1 car_sales["Price"] = car_sales["Price"].str.replace('[$,.]', '').astype(int)
File ~\Desktop\sample_project_1\env\Lib\site-packages\pandas\core\generic.py:6640, in NDFrame.astype(self, dtype, copy, errors)
6634 results = [
6635 ser.astype(dtype, copy=copy, errors=errors) for _, ser in self.items()
6636 ]
6638 else:
6639 # else, only a single dtype is given
-> 6640 new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
6641 res = self._constructor_from_mgr(new_data, axes=new_data.axes)
6642 return res.finalize(self, method="astype")
File ~\Desktop\sample_project_1\env\Lib\site-packages\pandas\core\internals\managers.py:430, in BaseBlockManager.astype(self, dtype, copy, errors)
427 elif using_copy_on_write():
428 copy = False
--> 430 return self.apply(
431 "astype",
432 dtype=dtype,
433 copy=copy,
434 errors=errors,
435 using_cow=using_copy_on_write(),
436 )
File ~\Desktop\sample_project_1\env\Lib\site-packages\pandas\core\internals\managers.py:363, in BaseBlockManager.apply(self, f, align_keys, **kwargs)
361 applied = b.apply(f, **kwargs)
362 else:
--> 363 applied = getattr(b, f)(**kwargs)
364 result_blocks = extend_blocks(applied, result_blocks)
366 out = type(self).from_blocks(result_blocks, self.axes)
File ~\Desktop\sample_project_1\env\Lib\site-packages\pandas\core\internals\blocks.py:758, in Block.astype(self, dtype, copy, errors, using_cow, squeeze)
755 raise ValueError("Can not squeeze with more than one column.")
756 values = values[0, :] # type: ignore[call-overload]
--> 758 new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
760 new_values = maybe_coerce_values(new_values)
762 refs = None
File ~\Desktop\sample_project_1\env\Lib\site-packages\pandas\core\dtypes\astype.py:237, in astype_array_safe(values, dtype, copy, errors)
234 dtype = dtype.numpy_dtype
236 try:
--> 237 new_values = astype_array(values, dtype, copy=copy)
238 except (ValueError, TypeError):
239 # e.g. _astype_nansafe can fail on object-dtype of strings
240 # trying to convert to float
241 if errors == "ignore":
File ~\Desktop\sample_project_1\env\Lib\site-packages\pandas\core\dtypes\astype.py:182, in astype_array(values, dtype, copy)
179 values = values.astype(dtype, copy=copy)
181 else:
--> 182 values = _astype_nansafe(values, dtype, copy=copy)
184 # in pandas we don't store numpy str dtypes, so convert to object
185 if isinstance(dtype, np.dtype) and issubclass(values.dtype.type, str):
File ~\Desktop\sample_project_1\env\Lib\site-packages\pandas\core\dtypes\astype.py:133, in _astype_nansafe(arr, dtype, copy, skipna)
129 raise ValueError(msg)
131 if copy or arr.dtype == object or dtype == object:
132 # Explicit copy, or required since NumPy can't view from / to object.
--> 133 return arr.astype(dtype, copy=True)
135 return arr.astype(dtype, copy=copy)
ValueError: invalid literal for int() with base 10: '$4,000.00'