我试图concat两个dataframes.当我尝试这样做,我被击中的错误,Reindexing only valid with uniquely valued Index objects
,而试图运行此代码:df_3=pd.concat([df_1,df_2])
。
我添加了以下检查和更新,以尝试识别问题:
df_1= df_1.reset_index(drop=True)
df_2= df_2.reset_index(drop=True)
try:
df_3=pd.concat([df_1,df_2], ignore_index=True)
except Exception as e:
print(df_1.index.is_unique)
print(df_1.index.to_list())
print(df_2.index.is_unique)
print(df_2.index.to_list())
df_1.to_csv("../../df_1.csv")
df_2.to_csv("../../df_2.csv")
raise Exception(e)
这将产生以下输出:
True
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 8
7, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123,
124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140]
True
[0, 1]
Reindexing only valid with uniquely valued Index objects
我不知道为什么会发生这种情况-我没有得到同样的问题时,我在一个控制台测试。请告知!
1条答案
按热度按时间9jyewag01#
错误是我有重复的列名-一个dataframe中的两个列具有相同的值。Pandas在这种情况下抛出相同的错误。感谢@Timeless找到这个问题!