前提・実現したいこと
アンケートベースのデータをクレンジングしています。
順調にフラグ立てをしていたのですが、
突然以下のエラーが起きました。
発生している問題・エラーメッセージ
MemoryError Traceback (most recent call last) <ipython-input-42-a99ba76878b8> in <module> 5 return 0 6 ----> 7 data['shampoo13'] = data.apply(lambda row:shampoo13(row), axis=1) c:\users\jupyter-notebook\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds) 6904 kwds=kwds, 6905 ) -> 6906 return op.get_result() 6907 6908 def applymap(self, func): c:\users\jupyter-notebook\lib\site-packages\pandas\core\apply.py in get_result(self) 184 return self.apply_raw() 185 --> 186 return self.apply_standard() 187 188 def apply_empty_result(self): c:\users\jupyter-notebook\lib\site-packages\pandas\core\apply.py in apply_standard(self) 290 291 # compute the result using the series generator --> 292 self.apply_series_generator() 293 294 # wrap results c:\users\jupyter-notebook\lib\site-packages\pandas\core\apply.py in apply_series_generator(self) 296 297 def apply_series_generator(self): --> 298 series_gen = self.series_generator 299 res_index = self.result_index 300 c:\users\jupyter-notebook\lib\site-packages\pandas\core\apply.py in series_generator(self) 399 return ( 400 constructor(arr, index=self.columns, name=name) --> 401 for i, (arr, name) in enumerate(zip(self.values, self.index)) 402 ) 403 pandas\_libs\properties.pyx in pandas._libs.properties.CachedProperty.__get__() c:\users\jupyter-notebook\lib\site-packages\pandas\core\apply.py in values(self) 132 @cache_readonly 133 def values(self): --> 134 return self.obj.values 135 136 @cache_readonly c:\users\jupyter-notebook\lib\site-packages\pandas\core\generic.py in values(self) 5441 ['monkey', nan, None]], dtype=object) 5442 """ -> 5443 self._consolidate_inplace() 5444 return self._data.as_array(transpose=self._AXIS_REVERSED) 5445 c:\users\jupyter-notebook\lib\site-packages\pandas\core\generic.py in _consolidate_inplace(self) 5251 self._data = self._data.consolidate() 5252 -> 5253 self._protect_consolidate(f) 5254 5255 def _consolidate(self, inplace=False): c:\users\jupyter-notebook\lib\site-packages\pandas\core\generic.py in _protect_consolidate(self, f) 5240 """ 5241 blocks_before = len(self._data.blocks) -> 5242 result = f() 5243 if len(self._data.blocks) != blocks_before: 5244 self._clear_item_cache() c:\usersjupyter-notebook\lib\site-packages\pandas\core\generic.py in f() 5249 5250 def f(): -> 5251 self._data = self._data.consolidate() 5252 5253 self._protect_consolidate(f) c:\users\jupyter-notebook\lib\site-packages\pandas\core\internals\managers.py in consolidate(self) 929 bm = self.__class__(self.blocks, self.axes) 930 bm._is_consolidated = False --> 931 bm._consolidate_inplace() 932 return bm 933 c:\users\jupyter-notebook\lib\site-packages\pandas\core\internals\managers.py in _consolidate_inplace(self) 934 def _consolidate_inplace(self): 935 if not self.is_consolidated(): --> 936 self.blocks = tuple(_consolidate(self.blocks)) 937 self._is_consolidated = True 938 self._known_consolidated = True c:\users\jupyter-notebook\lib\site-packages\pandas\core\internals\managers.py in _consolidate(blocks) 1910 for (_can_consolidate, dtype), group_blocks in grouper: 1911 merged_blocks = _merge_blocks( -> 1912 list(group_blocks), dtype=dtype, _can_consolidate=_can_consolidate 1913 ) 1914 new_blocks = _extend_blocks(merged_blocks, new_blocks) c:\users\jupyter-notebook\lib\site-packages\pandas\core\internals\blocks.py in _merge_blocks(blocks, dtype, _can_consolidate) 3319 3320 argsort = np.argsort(new_mgr_locs) -> 3321 new_values = new_values[argsort] 3322 new_mgr_locs = new_mgr_locs[argsort] 3323 MemoryError: Unable to allocate array with shape (14, 1123883) and data type int64
該当のソースコード
以下のようなコードをたくさん書いていたら起きてしまいました。
Python
1def age(row): 2 if row['a2'] == 0: 3 return'19歳以下' 4 elif row['a2'] == 1: 5 return'20~24歳' 6 elif row['a2'] == 2: 7 return '25~29歳' 8 elif row['a2'] == 3: 9 return '30~34歳' 10 elif row['a2'] == 4: 11 return '35~39歳' 12 elif row['a2'] == 5: 13 return '40~44歳' 14 elif row['a2'] == 6: 15 return '45~49歳' 16 else: 17 return '50歳以上' 18data['age'] = data.apply(age, axis=1)
試したこと
これを見つけましたが、
あまり理解できなかったし、
sklearnはここでは使っていなかったので、
別の解決方法があるのではと思いました!
何卒宜しくお願い致します。
###version
Windows 10 pro
プロセッサ Intel(R) Core(TM) i5-8265U @1.60GHz 1.80GHz
実装RAM8.00 GB (7.79GB使用可能)
(14, 1123883) の形状で int64 というこうとは、DataFrame 1個で125MB必要ということになるので、そのメモリ搭載量の PC では DataFrame のコピーを沢山作ったりするとメモリ不足になるかもしれないですね。