1 重新生成索引 如果某个索引值不存在就引入缺失值
1 from pandas import Series,DataFrame 2 import pandas as pd 3 import numpy as np 4 obj=Series([4.5,7.2,-5.3,3.6],index=['d','b','a','c']) 5 obj 6 7 #重新生成索引 8 obj2=obj.reindex(['a','b','c','d','e']) 9 obj2
a使用method的ffill可以实现前向值填充,效果如下
1 #前向填充 2 obj3=Series(['blue','purple','yellow'],index=[0,2,4]) 3 obj3.reindex(range(6),method='ffill')
b:对于dataframe使用reindex可以同时修改行列索引,如果仅传入一个序列那么如下
1 frame=DataFrame(np.arange(9).reshape((3,3)),index=['a','c','d'], 2 columns=['ohio','Texas','california']) 3 frame
1 frame2=frame.reindex(['a','b','c','d']) 2 frame2
c:使用colunms重新索引列
1 states=['Texax','Utah','california'] 2 frame.reindex(columns=states)