在学习Pandas时,当对一个Series或者DataFrame进行重新索引时,需要用到pandas.Series.reindex()或者pandas.DataFrame.redinx()。当某个索引值不存在时,会直接引入缺失值NaN。我们可以通过填充的方式,使Series或者DataFrame中新的index拥有新的索引。这篇文章整理下reindex中关于填充缺失值的几个方法
先创建一个object
import pandas as pd
obj = pd.Series(['a', 'b','c'], index=[0,1,4])
obj
#返回结果
Out[1]:
0 a
1 b
4 c
dtype: object
使用reindex()重新索引
obj.reindex