1、调用reindex方法将会根据新索引进行重排。如果某个索引值(对应索引的值)当前不存在,就引入缺失值,比如:
>> obj = Series([4.5,7,-7,4.3],index = ['d','b','a','c'])
>> obj
d 4.5
b 7
a -7
c 4.3
>> obj.reindex(['a','b','c','d','e'])
a -7
b 7
c 4.3
d 4.5
e NaN #索引值不存在引入索引值,not a number
>> obj.reindex(['a','b','c','d','e'],fill_value=0) #索引值不存在时进行插值处理
2、reindex方法中的method选项也可以做一些插值处理
>> obj2 = Series(['blue','purple','yellow'], index = ['0','2','4'])
>> obj2.reindex(range(6),method = 'ffill') #ffill是前向值填充,即让缺失值和前一项相同来填充,bfill是后向值填充
0 blue
1 blue
2 purple
3 purple
4 yellow
5 yellow
3、对于DataFrame,reindex可以修改行索引、列或者两个都修改。
>> frame = DataFrame(np.arange(9).reshape(3,3),index=['a','b','c'],columns=['him','her','it'])
him her it
a 0 1 2
b 3 4 5
c 6 7 8
>> frame.reindex(['a','b','c','d'])
him her it
a 0 1 2
b 3 4 5
c 6 7 8
d NaN NaN NaN
### 使用columns关键字即可重新索引列
>> states = ['him','her','them']
>> frame.reindex(columns = states)
him her them
a 0 1 NaN
b 3 4 NaN
c 6 7 NaN
### 也可以同时对行和列进行重新索引,而插值只能按行应用(即0轴)
>> frame.reindex(index = ['a','b','c','d'],method = 'ffill',columns = states)
him her them
a 0 1 NaN
b 3 4 NaN
c 6 7 NaN
d 6 7 NaN
###利用ix的标签索引功能,重新索引任务可以变得很简洁
>> frame.ix[['a','b','c','d'],states]
him her them
a 0 1 NaN
b 3 4 NaN
c 6 7 NaN
d NaN NaN NaN