在最新的 pandas 版本(0.8)中,set_index 是否发生了巨大变化?我无法让它按预期工作:
我最初的尝试试图在“id”上设置索引
ipdb> merged2['id']
16 130809
25 130687
32 130686
9 41736
22 131913
7 130691
33 129993
13 130680
28 134295
29 130708
ipdb> merged2.set_index('id')
*** KeyError: 0
ipdb> [type(i) for i in merged2['id']]
[<type 'numpy.float64'>, <type 'numpy.float64'>, <type 'numpy.float64'>, <type 'numpy.float64'>, <type 'numpy.float64'>, <type 'numpy.float64'>, <type 'numpy.float64'>, <type 'numpy.float64'>, <type 'numpy.float64'>, <type 'numpy.float64'>]
当前索引是 int:
ipdb> merged2.index
Int64Index([16, 25, 32, 9, 22, 7, 33, 13, 28, 29])
ipdb> [type(i) for i in merged2.index]
[<type 'numpy.int64'>, <type 'numpy.int64'>, <type 'numpy.int64'>, <type 'numpy.int64'>, <type 'numpy.int64'>, <type 'numpy.int64'>, <type 'numpy.int64'>, <type 'numpy.int64'>, <type 'numpy.int64'>, <type 'numpy.int64'>]
一种解决方法尝试构建一个新索引:
ndx=range(len(merged2))
[type(i) for i in ndx]
[<type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'int'>]
ipdb> merged2.set_index(ndx)
*** KeyError: 'no item named 0'
最后,将我的索引映射为 int 工作:
merged2['id']=map(lambda x: int(x), merged2['id']
merged2.set_index('id')
关于我做错了什么的想法?