How do I calculate the number of consecutive columns with zero values from the right until the first non zero element occurs
Asked Answered
S

3

5

Suppose I have the following dataframe:

   C1 C2 C3 C4  
0  1  2  3  0  
1  4  0  0  0  
2  0  0  0  3  
3  0  3  0  0 

Then I want to add another column such that it will display the number of zero valued column that occur contiguously from the right. The new column would be:

  Cnew  
0 1  
1 3  
2 0  
3 2  
Smear answered 23/7, 2017 at 4:25 Comment(0)
A
5

You can use:

  • reverse order by iloc and [::-1]
  • get cumsum per row (axis=1)
  • check eq and get sum of Trues

df['new'] = df.iloc[:,::-1].cumsum(axis=1).eq(0).sum(axis=1)
print (df)
   C1  C2  C3  C4  new
0   1   2   3   0    1
1   4   0   0   0    3
2   0   0   0   3    0
3   0   3   0   0    2
print (df.iloc[:,::-1])
   C4  C3  C2  C1
0   0   3   2   1
1   0   0   0   4
2   3   0   0   0
3   0   0   3   0


print (df.iloc[:,::-1].cumsum(axis=1))
   C4  C3  C2  C1
0   0   3   5   6
1   0   0   0   4
2   3   3   3   3
3   0   0   3   3

print (df.iloc[:,::-1].cumsum(axis=1).eq(0))
      C4     C3     C2     C1
0   True  False  False  False
1   True   True   True  False
2  False  False  False  False
3   True   True  False  False
Alacrity answered 23/7, 2017 at 4:28 Comment(4)
What is cumsum doing?Smear
it is cumulative sum, - mathworld.wolfram.com/CumulativeSum.htmlAlacrity
there is 3,2,1 and it count 0+3, 0+3+2, 0+3+2+1Alacrity
in last row 0,0+0, 0+0+3, 0+0+3+0Alacrity
A
4

I'd use argmax on a boolean array. Also, if I skip straight to numpy I can make this very fast.

(df.values[:, ::-1] != 0).argmax(1)

array([1, 3, 0, 2])

Or very similarly

(df.values[:, ::-1].astype(bool)).argmax(1)

array([1, 3, 0, 2])

I can place it in a new column with assign

df.assign(new=(df.values[:, ::-1] != 0).argmax(1))

   C1  C2  C3  C4  new
0   1   2   3   0    1
1   4   0   0   0    3
2   0   0   0   3    0
3   0   3   0   0    2

Or add a new column in place

df['new'] = (df.values[:, ::-1] != 0).argmax(1)
df

   C1  C2  C3  C4  new
0   1   2   3   0    1
1   4   0   0   0    3
2   0   0   0   3    0
3   0   3   0   0    2

Timing
We reduce the time by reducing the work necessary. We only need to find the position of the first non-zero.

# My first variant
%timeit df.assign(new=(df.values[:, ::-1] != 0).argmax(1))
# My second variant
%timeit df.assign(new=(df.values[:, ::-1].astype(bool)).argmax(1))
# jezrael's solution
%timeit df.assign(new=df.iloc[:,::-1].cumsum(1).eq(0).sum(1))
# numpy version of jezrael's solution
%timeit df.assign(new=(df.values[:,::-1].cumsum(1) == 0).sum(1))
# Scott Boston's solution
%timeit df.assign(new=df.iloc[:,::-1].eq(0).cumprod(axis=1).sum(axis=1))
# numpy version of Scott Boston's solution
%timeit df.assign(new=(df.values[:,::-1] == 0).cumprod(1).sum(1))

small data

1000 loops, best of 3: 301 µs per loop
1000 loops, best of 3: 273 µs per loop
1000 loops, best of 3: 770 µs per loop
1000 loops, best of 3: 323 µs per loop
1000 loops, best of 3: 647 µs per loop
1000 loops, best of 3: 324 µs per loop

larger data

df = pd.DataFrame(np.random.choice([0, 1], (10000, 100), p=(.7, .3)))

100 loops, best of 3: 6.03 ms per loop
100 loops, best of 3: 5.3 ms per loop
100 loops, best of 3: 16.9 ms per loop
100 loops, best of 3: 9 ms per loop
100 loops, best of 3: 10.7 ms per loop
100 loops, best of 3: 10.1 ms per loop
Antiperspirant answered 23/7, 2017 at 6:28 Comment(0)
I
3

Use eq, cumprod and sum (This is very similiar to a question answered here.)

df.iloc[:,::-1].eq(0).cumprod(axis=1).sum(axis=1)

Output:

0    1
1    3
2    0
3    2
dtype: int64
Iatrics answered 23/7, 2017 at 5:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.