1、調(diào)用reindex方法將會根據(jù)新索引進(jìn)行重排。如果某個索引值(對應(yīng)索引的值)當(dāng)前不存在,就引入缺失值,比如:
>> 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) #索引值不存在時進(jìn)行插值處理
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關(guān)鍵字即可重新索引列
>> states = ['him','her','them']
>> frame.reindex(columns = states)
him her them
a 0 1 NaN
b 3 4 NaN
c 6 7 NaN
### 也可以同時對行和列進(jìn)行重新索引,而插值只能按行應(yīng)用(即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的標(biāo)簽索引功能,重新索引任務(wù)可以變得很簡潔
>> 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