Pandas数据帧重新索引列issu

2024-09-28 17:01:45 发布

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

在过去的几个小时里我一直在想。。。

我有一个列表要用作数据帧的列:

totalColumns = [a, b, c, d, e, f.....z]

我有几个这样的数据帧:

数据框架一:

    b   f   j
1   12  5   6
2   4   99  2
3   10  77  16

数据框架二:

    a   k   y
1   2   25  46
2   7   54  76
3   34  67  101
4   45  24  54

还有更多。。。

我想根据totalColumns重新索引所有的数据帧。例如,在重新编制索引之后,DataFrameOne将如下所示:

数据框架一:

    a    b   c......f.....j......z
1   NaN  5   NaN....5.....6......NaN
2   NaN  99  NaN....99....2......NaN
3   NaN  77  NaN....77....16.....NaN

所以我用了reindex方法:

DataFrameOne.reindex(columns=totalColumns)

它对某些数据帧起作用,但对于某些数据帧,我会得到这个异常:

raise Exception('Reindexing only valid with uniquely valued Index '
Exception: Reindexing only valid with uniquely valued Index objects

有谁能帮我通过在某些数据帧上发生的这个错误吗?


Tags: 数据框架onlyindexwithexceptionnan小时