前提・実現したいこと
pythonでcsvファイルのカットをするため、dataframeを条件で分割したい。
そのためdataframeを動的にdf1,df2,df3,...と作りたい。
分割条件は2つの行(Cycle,Step)の数字を使う。
発生している問題・エラーメッセージ
python
1--------------------------------------------------------------------------- 2KeyError Traceback (most recent call last) 3~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 4 2645 try: 5-> 2646 return self._engine.get_loc(key) 6 2647 except KeyError: 7 8pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 9 10pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 11 12pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() 13 14pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() 15 16KeyError: 0 17 18During handling of the above exception, another exception occurred: 19 20KeyError Traceback (most recent call last) 21~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in set(self, item, value) 22 1070 try: 23-> 1071 loc = self.items.get_loc(item) 24 1072 except KeyError: 25 26~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 27 2647 except KeyError: 28-> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key)) 29 2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 30 31pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 32 33pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 34 35pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() 36 37pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() 38 39KeyError: 0 40 41During handling of the above exception, another exception occurred: 42 43ValueError Traceback (most recent call last) 44<ipython-input-7-f3ba3baa1b51> in <module> 45 1 for i in range(4): 46----> 2 df[i] = df[(df['Cycle'] == '1') & (df['Step'] == 'i')] 47 48~\anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value) 49 2936 else: 50 2937 # set column 51-> 2938 self._set_item(key, value) 52 2939 53 2940 def _setitem_slice(self, key, value): 54 55~\anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value) 56 2999 self._ensure_valid_index(value) 57 3000 value = self._sanitize_column(key, value) 58-> 3001 NDFrame._set_item(self, key, value) 59 3002 60 3003 # check if we are modifying a copy 61 62~\anaconda3\lib\site-packages\pandas\core\generic.py in _set_item(self, key, value) 63 3622 64 3623 def _set_item(self, key, value) -> None: 65-> 3624 self._data.set(key, value) 66 3625 self._clear_item_cache() 67 3626 68 69~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in set(self, item, value) 70 1072 except KeyError: 71 1073 # This item wasn't present, just insert at end 72-> 1074 self.insert(len(self.items), item, value) 73 1075 return 74 1076 75 76~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in insert(self, loc, item, value, allow_duplicates) 77 1179 new_axis = self.items.insert(loc, item) 78 1180 79-> 1181 block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1)) 80 1182 81 1183 for blkno, count in _fast_count_smallints(self._blknos[loc:]): 82 83~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in make_block(values, placement, klass, ndim, dtype) 84 3039 values = DatetimeArray._simple_new(values, dtype=dtype) 85 3040 86-> 3041 return klass(values, ndim=ndim, placement=placement) 87 3042 88 3043 89 90~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim) 91 2587 values = np.array(values, dtype=object) 92 2588 93-> 2589 super().__init__(values, ndim=ndim, placement=placement) 94 2590 95 2591 @property 96 97~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim) 98 123 if self._validate_ndim and self.ndim and len(self.mgr_locs) != len(self.values): 99 124 raise ValueError( 100--> 125 f"Wrong number of items passed {len(self.values)}, " 101 126 f"placement implies {len(self.mgr_locs)}" 102 127 ) 103 104ValueError: Wrong number of items passed 8, placement implies 1 105 106
該当のソースコード
python
1for i in range(4): 2 df[i] = df[(df['Cycle'] == '1') & (df['Step'] == 'i')]
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。