问题
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.nan
s 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 NaN
s forces the dtype of first
to be promoted from an integer dtype to a floating-point dtype since NaN
s are floats (and hence Series of integer dtype can not contain NaN
s).
来源:https://stackoverflow.com/questions/38743064/pandas-pad-series-on-top-or-bottom