Pandas: pad series on top or bottom

断了今生、忘了曾经 提交于 2019-12-23 03:32:21

问题


This turned out to be non-trivial for me so I wanted to check if others have a simple solution for this:

Suppose I have an arbitrary number (say 3) of pd.Series: which look like:

first = pd.Series(range(5))
second = pd.Series(range(7))
third = pd.Series(range(6))

I'd like to make them all of the same length (7 -- which is the largest length) and pad the shorter ones with np.nans either at the top (optionally at the bottom) so that first looks like:

nan
nan
  0
  1
  2
  3
  4

and so on.


回答1:


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).



来源:https://stackoverflow.com/questions/38743064/pandas-pad-series-on-top-or-bottom

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