可变指数?Python Dataframe ValueError:无法从重复轴重新索引

2024-07-04 07:59:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个具有多个重复值的数据帧作为索引,例如: enter image description here

我需要将350包含的值拆分为351352353,依此类推。。。 我想改变索引的值,给它们一个唯一的值,以便对它们进行唯一的操作。我尝试更改索引,但出现以下错误: enter image description here

我的代码的目的是重新编制索引并仅获取列表中的内容的值。 最好的办法是什么?有没有办法更改索引值以便我可以处理数据帧

my_finallist = [1,2,3,4,5,6,7]
data_backup.reindex(my_finallist)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-156-2af1a8aae353> in <module>
----> 1 data_backup.reindex(my_finallist)

c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    310         @wraps(func)
    311         def wrapper(*args, **kwargs) -> Callable[..., Any]:
--> 312             return func(*args, **kwargs)
    313 
    314         kind = inspect.Parameter.POSITIONAL_OR_KEYWORD

c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in reindex(self, *args, **kwargs)
   4171         kwargs.pop("axis", None)
   4172         kwargs.pop("labels", None)
-> 4173         return super().reindex(**kwargs)
   4174 
   4175     def drop(

c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\generic.py in reindex(self, *args, **kwargs)
   4806 
   4807         # perform the reindex on the axes
-> 4808         return self._reindex_axes(
   4809             axes, level, limit, tolerance, method, fill_value, copy
   4810         ).__finalize__(self, method="reindex")

c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)
   4017         index = axes["index"]
   4018         if index is not None:
-> 4019             frame = frame._reindex_index(
   4020                 index, method, copy, level, fill_value, limit, tolerance
   4021             )

c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in _reindex_index(self, new_index, method, copy, level, fill_value, limit, tolerance)
   4036             new_index, method=method, level=level, limit=limit, tolerance=tolerance
   4037         )
-> 4038         return self._reindex_with_indexers(
   4039             {0: [new_index, indexer]},
   4040             copy=copy,

c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\generic.py in _reindex_with_indexers(self, reindexers, fill_value, copy, allow_dups)
   4872 
   4873             # TODO: speed up on homogeneous DataFrame objects
-> 4874             new_data = new_data.reindex_indexer(
   4875                 index,
   4876                 indexer,

c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\internals\managers.py in reindex_indexer(self, new_axis, indexer, axis, fill_value, allow_dups, copy, consolidate, only_slice)
   1299         # some axes don't allow reindexing with dups
   1300         if not allow_dups:
-> 1301             self.axes[axis]._can_reindex(indexer)
   1302 
   1303         if axis >= self.ndim:

c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in _can_reindex(self, indexer)
   3474         # trying to reindex on an axis with duplicates
   3475         if not self._index_as_unique and len(indexer):
-> 3476             raise ValueError("cannot reindex from a duplicate axis")
   3477 
   3478     def reindex(self, target, method=None, level=None, limit=None, tolerance=None):

ValueError: cannot reindex from a duplicate axis

Tags: inpyselfpandasindexlibpackageslocal
1条回答
网友
1楼 · 发布于 2024-07-04 07:59:24

使用rename也可以处理重复的索引值:

df = df.rename(index={833:778})
#index is default value, so possible use
#df = df.rename({833:778})

如果需要索引值的计数器(但可能创建了新的副本):

df.index += df.groupby(level=0).cumcount()

#working like
#df.index = df.groupby(level=0).cumcount() + df.index

相关问题 更多 >

    热门问题