You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pd.concat合并DataFrame时遭遇'Reindexing only valid with uniquely valued index objects'错误的求助

使用pd.concat合并DataFrame时遭遇'Reindexing only valid with uniquely valued index objects'错误的求助

我已经找到几篇和这个问题相关的文章,但都没能解决我的问题。

我有两组Python DataFrame:df_A1、df_A2 和 df_B1、df_B2,我想把A组的两个DataFrame合并到一起,B组的两个也合并起来。合并A组的两个DataFrame完全没问题,但合并B组的时候却抛出了错误:'Reindexing only valid with uniquely valued index objects'。我在网上找了几个可能的诱因,但都不适用我的情况:

  • 重复列名:
    B组的DataFrame确实存在重复列名,但A组的DataFrame也有同样的情况,而且合并A组完全正常,所以这应该不是问题所在。另外我用df.index.is_unique检查了所有DataFrame,返回结果都是True,当然df.columns.is_unique返回的是False。

  • 空列名:
    我之前确实有空列名的情况,但已经用[item or '_empty_' for item in <list_columns>]把所有空列名都填充好了。

  • 使用ignore_index=True参数:
    不管是合并A组还是B组我都添加了这个参数,A组合并依然正常工作,但B组还是报同样的错误。

下面是完整的错误回溯信息:

Traceback (most recent call last):

  File ~\AppData\Local\anaconda3\Lib\site-packages\spyder_kernels\customize\utils.py:209 in exec_encapsulate_locals
    exec_fun(compile(code_ast, filename, "exec"), globals)

  File c:\<path>\python_script.py:168
    df_B = pd.concat([df_B1 , df_B2], axis=0, ignore_index=True)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\core\reshape\concat.py:395 in concat
    return op.get_result()

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\core\reshape\concat.py:680 in get_result
    indexers[ax] = obj_labels.get_indexer(new_labels)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3885 in get_indexer
    raise InvalidIndexError(self._requires_unique_msg)

InvalidIndexError: Reindexing only valid with uniquely valued Index objects

有没有大佬知道该怎么解决这个问题?

备注:内容来源于stack exchange,提问作者Fish1996

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 18:08:10