填充具有后续值的数组

时间:2016-10-22 08:53:26

标签: python arrays python-2.7 pandas numpy

在我的数据框架中,我将得到一个只有很少非纳米值的列。我想使用非nan值作为包含NaN值的所有前面行的分组变量。为了模拟它,我做了以下数组:

count = np.array([np.NaN,np.NaN,np.NaN,3,np.NaN,np.NaN,6,np.NaN,np.NaN,np.NaN,np.NaN,np.NaN,12])
count = Series(count)

对于这个数组,我能够创建一个填充函数

def pad_expsamp_time(array):
    sect = np.zeros(array.size) # create array filled with zeros
    inds = array.index[array.notnull()] # select the non-zero values
    rev_inds = inds[::-1] # sort high to low
    # fill array with value until index of value. Repeat for lower values. 
    for i in rev_inds: 
        sect[:i] = i
    return Series(sect)

当它可以假设非纳米值的索引等于实际值时,此函数有效。但是,当索引不等于内容时,如何填充数组?

例如,如果数组计数为:

count = np.array([np.NaN,np.NaN,np.NaN,1,np.NaN,np.NaN,2,np.NaN,np.NaN,np.NaN,np.NaN,np.NaN,3])

所需的输出是

count = np.array([1,1,1,1,2,2,2,3,3,3,3,3,3]

阵列末端可能有NaN。我希望这些留下NaNs,以便数据框将忽略它们。

count = np.array([np.NaN,np.NaN,np.NaN,1,np.NaN,np.NaN,2,np.NaN,np.NaN,3,np.NaN,np.NaN]) 
# Will become:
count = np.array([1,1,1,1,2,2,2,3,3,3,np.nan,np.nan]

2 个答案:

答案 0 :(得分:2)

这是一种矢量化方法 -

# Append False at either sides of NaN mask as we try to find start &
# stop of each NaN interval by looking for rising and falling edges
mask = np.hstack((False,np.isnan(count),False))
start = np.flatnonzero(mask[1:] > mask[:-1])
stop = np.flatnonzero(mask[1:] < mask[:-1])
lens = stop - start

# Account for NaNs if any at the end of input that might throw off stop values
stop = stop.clip(max=count.size-1)

# Assign values
count[mask[1:-1]] = count[stop].repeat(lens)

样品运行 -

案例#1:

In [103]: count
Out[103]: 
array([ nan,  nan,  nan,   6.,  nan,  nan,   5.,  nan,  nan,  nan,  nan,
        nan,   2.])

In [104]:  # Listed code ...

In [105]: count
Out[105]: array([ 6.,  6.,  6.,  6.,  5.,  5.,  5.,  2.,  2.,  2.,  2.,  2.,  2.])

案例#2:

In [118]: count
Out[118]: 
array([ nan,  nan,  nan,   1.,  nan,  nan,   2.,  nan,  nan,  nan,  nan,
        nan,   3.])

In [119]:   # Listed code ...

In [120]: count
Out[120]: array([ 1.,  1.,  1.,  1.,  2.,  2.,  2.,  3.,  3.,  3.,  3.,  3.,  3.])

案例#3:

In [114]: count
Out[114]: 
array([ nan,  nan,  nan,   1.,  nan,  nan,   2.,  nan,  nan,   3.,  nan,
        nan])

In [115]:   # Listed code ...

In [116]: count
Out[116]: 
array([  1.,   1.,   1.,   1.,   2.,   2.,   2.,   3.,   3.,   3.,  nan,
        nan])

答案 1 :(得分:2)

IIUC你可以简单地使用pandas bfill()方法:

你的样本:

In [89]: s = pd.Series(np.array([np.nan,np.nan,np.nan,1,np.nan,np.nan,2,np.nan,np.nan,3,np.nan,np.nan]))

In [90]: s
Out[90]:
0     NaN
1     NaN
2     NaN
3     1.0
4     NaN
5     NaN
6     2.0
7     NaN
8     NaN
9     3.0
10    NaN
11    NaN
dtype: float64

In [91]: s.bfill()
Out[91]:
0     1.0
1     1.0
2     1.0
3     1.0
4     2.0
5     2.0
6     2.0
7     3.0
8     3.0
9     3.0
10    NaN
11    NaN
dtype: float64
Divakar的样本:

In [81]: s = pd.Series(array([ nan,  nan,  nan,   6.,  nan,  nan,   5.,  nan,  nan,  nan,  nan, nan,   2.]))

In [82]: s
Out[82]:
0     NaN
1     NaN
2     NaN
3     6.0
4     NaN
5     NaN
6     5.0
7     NaN
8     NaN
9     NaN
10    NaN
11    NaN
12    2.0
dtype: float64

In [83]: s.bfill()
Out[83]:
0     6.0
1     6.0
2     6.0
3     6.0
4     5.0
5     5.0
6     5.0
7     2.0
8     2.0
9     2.0
10    2.0
11    2.0
12    2.0
dtype: float64

In [84]: s = pd.Series(array([ nan,  nan,  nan,   1.,  nan,  nan,   2.,  nan,  nan,  nan,  nan, nan, 3.]))

In [85]: s.bfill()
Out[85]:
0     1.0
1     1.0
2     1.0
3     1.0
4     2.0
5     2.0
6     2.0
7     3.0
8     3.0
9     3.0
10    3.0
11    3.0
12    3.0
dtype: float64

In [86]: s = pd.Series(array([ nan, nan, nan, 1., nan, nan, 2., nan, nan, 3., nan, nan]))

In [87]: s.bfill()
Out[87]:
0     1.0
1     1.0
2     1.0
3     1.0
4     2.0
5     2.0
6     2.0
7     3.0
8     3.0
9     3.0
10    NaN
11    NaN
dtype: float64