OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I form groups by a mask and N rows after that mask?

  • Thread starter Thread starter AmirX
  • Start date Start date
A

AmirX

Guest
My DataFrame is:

Code:
import pandas as pd
df = pd.DataFrame(
    {
        'a': [False, True, False, True, False, True, False, True, True, False, False],
    }
)

Expected output is forming groups like this:

Code:
        a
1    True
2   False
3    True

        a
5    True
6   False
7    True

      a
8    True
9   False
10  False

The logic is:

Basically I want to form groups where df.a == True and two rows after that. For example, in order to create the first group, the first True should be found which is row 1. Then the first group is rows 1, 2 and 3. For the second group the next True must be found which is not in the first group. That row is row 5. So the second group is consisted of rows 5, 6 and 7. This image clarifies the point:

enter image description here

And this is my attempt that didn't work:

Code:
N = 2
mask = ((df.a.eq(True))
        .cummax().cumsum()
        .between(1, N+1)
        )

out = df[mask]
<p>My <strong>DataFrame</strong> is:</p>
<pre><code>import pandas as pd
df = pd.DataFrame(
{
'a': [False, True, False, True, False, True, False, True, True, False, False],
}
)
</code></pre>
<p><strong>Expected output</strong> is forming groups like this:</p>
<pre><code> a
1 True
2 False
3 True

a
5 True
6 False
7 True

a
8 True
9 False
10 False
</code></pre>
<p><strong>The logic is:</strong></p>
<p>Basically I want to form groups where <code>df.a == True</code> and two rows after that. For example, in order to create the first group, the first <code>True</code> should be found which is row <code>1</code>. Then the first group is rows 1, 2 and 3. For the second group the next <code>True</code> must be found which is not in the first group. That row is row <code>5</code>. So the second group is consisted of rows 5, 6 and 7. This image clarifies the point:</p>
<p><a href="https://i.sstatic.net/rE2vDRtk.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rE2vDRtk.png" alt="enter image description here" /></a></p>
<p>And this is my attempt that didn't work:</p>
<pre><code>N = 2
mask = ((df.a.eq(True))
.cummax().cumsum()
.between(1, N+1)
)

out = df[mask]
</code></pre>
 
Top