Я пытаюсь удалить все нечисловые символы c из моего кадра данных (например, такие символы, как] $ ^ M # et c.) Одной строкой кода. Фрейм данных представляет собой набор данных приложений Google Play Store.
df = pd.read_csv("googleplaystore.csv")
df['Rating'].fillna(value = '0.0', inplace = True)
#sample data#
Rating Reviews Size Installs Type Price \
0 4.1 159 19M 10,000+ Free 0
1 3.9 967 14M 500,000+ Free 0
2 4.7 87510 8.7M 5,000,000+ Free 0
3 4.5 215644 25M 50,000,000+ Free 0
4 4.3 967 2.8M 100,000+ Free 0
... ... ... ... ... ... ...
10836 4.5 38 53M 5,000+ Free 0
10837 5 4 3.6M 100+ Free 0
10838 0.0 3 9.5M 1,000+ Free 0
10839 4.5 114 Varies with device 1,000+ Free 0
10840 4.5 398307 19M 10,000,000+ Free 0
Content Rating Genres Last Updated \
0 Everyone Art & Design January 7, 2018
1 Everyone Art & Design;Pretend Play January 15, 2018
2 Everyone Art & Design August 1, 2018
3 Teen Art & Design June 8, 2018
4 Everyone Art & Design;Creativity June 20, 2018
... ... ... ...
10836 Everyone Education July 25, 2017
10837 Everyone Education July 6, 2018
10838 Everyone Medical January 20, 2017
10839 Mature 17+ Books & Reference January 19, 2015
10840 Everyone Lifestyle July 25, 2018
clean_data = df.replace('[^\d.]', '', regex = True).astype(float)
По сути, я пытаюсь удалить букву «M» из столбца «Размер» после цифр, а также Знак '+' в столбце Установки.
Но я вернулся с этим сообщением об ошибке;
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-325-887d47a9889e> in <module>
----> 1 data_ = df.replace('[^\d.]', '', regex = True).astype(float)
~\anaconda3\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors)
5696 else:
5697 # else, only a single dtype is given
-> 5698 new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors)
5699 return self._constructor(new_data).__finalize__(self)
5700
~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in astype(self, dtype, copy, errors)
580
581 def astype(self, dtype, copy: bool = False, errors: str = "raise"):
--> 582 return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
583
584 def convert(self, **kwargs):
~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, filter, **kwargs)
440 applied = b.apply(f, **kwargs)
441 else:
--> 442 applied = getattr(b, f)(**kwargs)
443 result_blocks = _extend_blocks(applied, result_blocks)
444
~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors)
623 vals1d = values.ravel()
624 try:
--> 625 values = astype_nansafe(vals1d, dtype, copy=True)
626 except (ValueError, TypeError):
627 # e.g. astype_nansafe can fail on object-dtype of strings
~\anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
895 if copy or is_object_dtype(arr) or is_object_dtype(dtype):
896 # Explicit copy, or required since NumPy can't view from / to object.
--> 897 return arr.astype(dtype, copy=True)
898
899 return arr.view(dtype)
ValueError: could not convert string to float:
Пожалуйста, помогите в отладке, если это возможно. Я действительно хотел бы сохранить его в одной строке кода для всего фрейма данных. Заранее спасибо.