重新索引
reindex重置索引,如果索引值不存在,就引入缺失值
参数介绍
参数 |
说明 |
index |
用作索引的新序列 |
method |
插值 |
fill_vlaue |
引入缺失值时的替代NaN |
limit |
最大填充量 |
level |
指定级别上匹配简单索引,否则选取子集 |
copy |
默认为True |
实例:
import pandas as pd
import numpy as np
from pandas import Series
obj = Series([4.5,7.2,-5.3,3.6],index=['d','b','a','c'])
obj
d 4.5
b 7.2
a -5.3
c 3.6
dtype: float64
obj2 = obj.reindex(['a','b','c','d','e'])
obj2
a -5.3
b 7.2
c 3.6
d 4.5
e NaN
dtype: float64
既然有了缺失值,那么怎么填充,下面这方法
obj.reindex(['a','b','c','d','e'],fill_value=0)
a -5.3
b 7.2
c 3.6
d 4.5
e 0.0
dtype: float64
对于DataFrame,reindex可以修改行索引,列索引或者都修改,默认重新索引行
frame = pd.DataFrame(np.arange(9).reshape(3,3),index=['b','c','a'])
frame.reindex(index=['a','b','c','d'],columns=[2,1,0])
2 1 0
a 8.0 7.0 6.0
b 2.0 1.0 0.0
c 5.0 4.0 3.0
d NaN NaN NaN
利用ix的标签索引功能
frame.ix[['d','c','b','a'],[0,1,2]]
0 1 2
d NaN NaN NaN
c 3.0 4.0 5.0
b 0.0 1.0 2.0
a 6.0 7.0 8.0