pandas :垫系列在顶部或底部 [英] Pandas: pad series on top or bottom

查看:80
本文介绍了 pandas :垫系列在顶部或底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我来说,这实在是不平凡的,所以我想检查一下其他人是否对此有一个简单的解决方案:

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

假设我有一个任意数量的(例如3)pd.Series:看起来像:

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

我想让它们都具有相同的长度(7-这是最大的长度),并在顶部(可选地在底部)上用np.nan填充较短的那些,以便使第一个看起来像:

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

以此类推.

推荐答案

您可以使用

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

您可以通过选择重新索引标签来控制NaN的放置:

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

请注意,包含NaN会强制将first的dtype从整数dtype提升为浮点dtype,因为NaN都是浮点数(因此,整数dtype系列不能包含NaN s).

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

这篇关于 pandas :垫系列在顶部或底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆