【发布时间】:2012-10-09 09:21:49
【问题描述】:
过去几个小时我一直在尝试解决这个问题......
我有一个列表,我想用作 DataFrame 的列:
totalColumns = [a, b, c, d, e, f.....z]
我有几个看起来像这样的数据框:
DataFrameOne:
b f j
1 12 5 6
2 4 99 2
3 10 77 16
DataFrameTwo:
a k y
1 2 25 46
2 7 54 76
3 34 67 101
4 45 24 54
还有更多...
我想根据 totalColumns 重新索引所有数据框。例如,重新索引后,DataFrameOne 将如下所示:
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
谁能帮我解决在某些数据帧上发生的这个错误?
【问题讨论】: