環境:win10,python3.6
データフレーム内にあるカンマ区切り有のobjectデータを、カンマ区切りなしのintに変換したいと考えています。
#例: "20,000" → 20000
import pandas as pd data1 = pd.read_csv(r"data.csv",engine = "python") print(data1["A"].dtype) #out:object, "20,000"のような形 data1["A"] = data1["A"].replace(',','').astype(np.int)
すると、以下のようなエラーを得ました。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-60-bef64f82d78f> in <module>() ----> 1 data1["A"] = data1["A"].replace(',','').astype(np.int) /usr/local/lib/python3.6/dist-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs) 116 else: 117 kwargs[new_arg_name] = new_arg_value --> 118 return func(*args, **kwargs) 119 return wrapper 120 return _deprecate_kwarg /usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs) 4002 # else, only a single dtype is given 4003 new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors, -> 4004 **kwargs) 4005 return self._constructor(new_data).__finalize__(self) 4006 /usr/local/lib/python3.6/dist-packages/pandas/core/internals.py in astype(self, dtype, **kwargs) 3460 3461 def astype(self, dtype, **kwargs): -> 3462 return self.apply('astype', dtype=dtype, **kwargs) 3463 3464 def convert(self, **kwargs): /usr/local/lib/python3.6/dist-packages/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs) 3327 3328 kwargs['mgr'] = self -> 3329 applied = getattr(b, f)(**kwargs) 3330 result_blocks = _extend_blocks(applied, result_blocks) 3331 /usr/local/lib/python3.6/dist-packages/pandas/core/internals.py in astype(self, dtype, copy, errors, values, **kwargs) 542 def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs): 543 return self._astype(dtype, copy=copy, errors=errors, values=values, --> 544 **kwargs) 545 546 def _astype(self, dtype, copy=False, errors='raise', values=None, /usr/local/lib/python3.6/dist-packages/pandas/core/internals.py in _astype(self, dtype, copy, errors, values, klass, mgr, **kwargs) 623 624 # _astype_nansafe works fine with 1-d only --> 625 values = astype_nansafe(values.ravel(), dtype, copy=True) 626 values = values.reshape(self.shape) 627 /usr/local/lib/python3.6/dist-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy) 690 elif arr.dtype == np.object_ and np.issubdtype(dtype.type, np.integer): 691 # work around NumPy brokenness, #1987 --> 692 return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape) 693 694 if dtype.name in ("datetime64", "timedelta64"): pandas/_libs/lib.pyx in pandas._libs.lib.astype_intsafe() pandas/_libs/src/util.pxd in util.set_value_at_unsafe() ValueError: invalid literal for int() with base 10: '97,750'
どう直したらいいのかよくわからないので、教えて下さると幸いです。よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。