我正在尝试向现有的多索引数据帧添加新行。当我尝试执行此操作时,出现ValueError:无法处理非唯一的多索引!但是,当我尝试读取相同的索引时,会收到一个KeyError,向我显示该索引不存在(因此应该是唯一的?)。
df.loc[('shntswt_40465_1__22',50.0),variables] = values[:-1]
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
df.loc[('shntswt_40465_1__22',50.0),variables] = values[:-1]
File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 190, in __setitem__
self._setitem_with_indexer(indexer, value)
File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 376, in _setitem_with_indexer
self.obj._data = self.obj.reindex(labels, axis=i)._data
File "C:\Python37\lib\site-packages\pandas\util\_decorators.py", line 197, in wrapper
return func(*args, **kwargs)
File "C:\Python37\lib\site-packages\pandas\core\frame.py", line 3809, in reindex
return super(DataFrame, self).reindex(**kwargs)
File "C:\Python37\lib\site-packages\pandas\core\generic.py", line 4356, in reindex
fill_value, copy).__finalize__(self)
File "C:\Python37\lib\site-packages\pandas\core\frame.py", line 3741, in _reindex_axes
fill_value, limit, tolerance)
File "C:\Python37\lib\site-packages\pandas\core\frame.py", line 3749, in _reindex_index
tolerance=tolerance)
File "C:\Python37\lib\site-packages\pandas\core\indexes\multi.py", line 2227, in reindex
raise ValueError("cannot handle a non-unique multi-index!")
ValueError: cannot handle a non-unique multi-index!
df.loc[('shntswt_40465_1__22',50.0),variables]
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
df.loc[('shntswt_40465_1__22',50.0),variables]
File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 1494, in __getitem__
return self._getitem_tuple(key)
File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 868, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 969, in _getitem_lowerdim
return self._getitem_nested_tuple(tup)
File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 1048, in _getitem_nested_tuple
obj = getattr(obj, self.name)._getitem_axis(key, axis=axis)
File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 1913, in _getitem_axis
return self._get_label(key, axis=axis)
File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 141, in _get_label
return self.obj._xs(label, axis=axis)
File "C:\Python37\lib\site-packages\pandas\core\generic.py", line 3583, in xs
drop_level=drop_level)
File "C:\Python37\lib\site-packages\pandas\core\indexes\multi.py", line 2542, in get_loc_level
return partial_selection(key)
File "C:\Python37\lib\site-packages\pandas\core\indexes\multi.py", line 2532, in partial_selection
indexer = self.get_loc(key)
File "C:\Python37\lib\site-packages\pandas\core\indexes\multi.py", line 2418, in get_loc
raise KeyError(key)
KeyError: ('shntswt_40465_1__22', 50.0)
但是我可以检索并分配给现有行。
df.loc[('trf_91000_91005_1',50.0)]
m:R m:X m:Z m:phiz
Contingency Freq
trf_91000_91005_1 50.0 NaN NaN NaN NaN
df.loc[('trf_91000_91005_1',50.0),variables] = values[:-1]
df.loc[('trf_91000_91005_1',50.0)]
m:R m:X m:Z m:phiz
Contingency Freq
trf_91000_91005_1 50.0 2.85944 10.5443 10.9252 74.8273
我敢肯定,我之前已经使用.loc创建了新的多索引条目,但是我看不到哪里出了错。