我有一个数据框,它是通过将多个数据框一起附加到一个长列表中生成的。如图,默认索引是0~7之间的循环,因为每个原始df都有这个索引。总行数为 240。那么如何将新的 df 重新索引为 0~239 而不是 30 x 0~7。
我尝试了 df.reset_index(drop=True),但它似乎不起作用。我也试过:df.reindex(np.arange(240)) 但它返回错误
ValueError: cannot reindex from a duplicate axis
请您参考如下方法:
您似乎忘记了分配输出,因为默认情况下 reset_index不工作 inplace:
df = df.reset_index(drop=True)
或者:
df.reset_index(drop=True, inplace=True)
但更好的解决方案是(如果使用 concat )添加参数 ignore_index=True:
df = pd.concat([df1, df2, ..., df7], ignore_index=True)
