Counting non-overlapping runs of non-zero values ​​per row in a DataFrame

Let's say I have the following Pandas DataFrame:

id | a1 | a2 | a3 | a4 
1  | 3  | 0  | 10 | 25   
2  | 0  | 0  | 31 | 15  
3  | 20 | 11 | 6  | 5  
4  | 0  | 3  | 1  | 7  

I want to calculate the number of non-overlapping runs of nconsecutive non-zero values ​​in each row for different values n. Desired Result:

id | a1 | a2 | a3 | a4 | 2s | 3s | 4s
1  | 3  | 0  | 10 | 25 | 1  | 0  | 0
2  | 0  | 0  | 31 | 15 | 1  | 0  | 0
3  | 20 | 11 | 6  | 5  | 2  | 1  | 1
4  | 0  | 3  | 1  | 7  | 1  | 1  | 0

where, for example, each value in the column 2sshows the number of non-overlapping runs of length 2 in this row, each value in the column 3sshows the corresponding number of runs of length 3, etc.

I wonder if there are any Pandas or Numpy methods to take care of this?

+4
source share
2 answers

2D convolution -

from scipy.signal import convolve2d as conv2

n = 6
v = np.vstack([(conv2(df.values!=0,[[1]*I])==I).sum(1) for I in range(2,n+1)]).T
df_v = pd.DataFrame(v, columns = [[str(i)+'s' for i in range(2,n+1)]])
df_out = pd.concat([df, df_v],1)

, non- . , , . , 3 . , , -, 3. , , 3 . ! , 2s, 3s ..

3s -

In [326]: a
Out[326]: 
array([[0, 2, 1, 2, 1, 2],
       [2, 2, 2, 0, 0, 0],
       [2, 2, 1, 1, 1, 1],
       [1, 2, 1, 2, 0, 1]])

In [327]: a!=0
Out[327]: 
array([[False,  True,  True,  True,  True,  True],
       [ True,  True,  True, False, False, False],
       [ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True, False,  True]], dtype=bool)

In [329]: conv2(a!=0,[[1]*3])
Out[329]: 
array([[0, 1, 2, 3, 3, 3, 2, 1],
       [1, 2, 3, 2, 1, 0, 0, 0],
       [1, 2, 3, 3, 3, 3, 2, 1],
       [1, 2, 3, 3, 2, 2, 1, 1]])

In [330]: conv2(a!=0,[[1]*3])==3
Out[330]: 
array([[False, False, False,  True,  True,  True, False, False],
       [False, False,  True, False, False, False, False, False],
       [False, False,  True,  True,  True,  True, False, False],
       [False, False,  True,  True, False, False, False, False]], dtype=bool)

In [331]: (conv2(a!=0,[[1]*3])==3).sum(1)
Out[331]: array([3, 1, 4, 2])

-

In [158]: df_out
Out[158]: 
   a1  a2  a3  a4  a5  a6  2s  3s  4s  5s  6s
0   1   2   1   0   0   2   2   1   0   0   0
1   1   1   2   1   0   1   3   2   1   0   0
2   1   1   0   0   1   1   2   0   0   0   0
3   2   2   1   0   2   2   3   1   0   0   0

, 'id', . , df.values[:,1:] df.values .

+4

, .

def count(row,mins):
    runs=(row!=0).astype(uint8).tobytes().decode().split(chr(0))
    lengths=[len(run) for run in runs]
    return np.floor_divide.outer(lengths,mins).sum(0) 

, , //, , .

df:

    a1  a2  a3  a4
id                
1    3   0  10  25
2    0   0  31  15
3   20  11   6   5
4    0   3   1   7

np.apply_along_axis(count,1,df,[2,3,4])

array([[1, 0, 0],
       [1, 0, 0],
       [2, 1, 1],
       [1, 1, 0]], dtype=int32)

df.

+1

Source: https://habr.com/ru/post/1668168/


All Articles