Pandas: pad series on top or bottom

陌路散爱 提交于 2019-12-06 15:52:03

You could use reindex to give each Series a new index. If the new index contains labels which are not in the original series' index, then a NaN value is filled in (unless a different fill_value is specified):

In [15]: first.reindex(range(7))
Out[15]: 
0    0.0
1    1.0
2    2.0
3    3.0
4    4.0
5    NaN
6    NaN
dtype: float64

You can control the placement of the NaNs by your choice of reindexing labels:

In [19]: first.reindex(range(-2,5))
Out[19]: 
-2    NaN
-1    NaN
 0    0.0
 1    1.0
 2    2.0
 3    3.0
 4    4.0
dtype: float64

Note that the inclusion of NaNs forces the dtype of first to be promoted from an integer dtype to a floating-point dtype since NaNs are floats (and hence Series of integer dtype can not contain NaNs).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!