title: Pandas longest final stretch within each group
body: <p>I have the following Pandas dataframe:</p>
  <pre><code>import pandas as pd
  
      df = pd.DataFrame(
      
          [
              (&quot;bird&quot;, '2022-01',&quot;Falconiformes&quot;),
              (&quot;bird&quot;, '2022-02',&quot;Falconiformes&quot;),
              (&quot;bird&quot;, '2022-03',&quot;Falconiformes&quot;),
              (&quot;bird&quot;, '2022-04',&quot;Falconiformes&quot;),
              (&quot;bird&quot;, '2022-05',&quot;Falconiformes&quot;),
              (&quot;bird&quot;, '2022-06',&quot;Falconiformes&quot;),
              (&quot;bird&quot;, '2022-07',&quot;Falconiformes&quot;),
              (&quot;bird&quot;, '2022-08',&quot;Falconiformes&quot;),
              (&quot;bird&quot;, '2022-09',&quot;Psittaciformes&quot;),
              (&quot;bird&quot;, '2022-10',&quot;Psittaciformes&quot;),
              (&quot;bird&quot;, '2022-11',&quot;Psittaciformes&quot;),
              (&quot;bird&quot;, '2022-12',&quot;Psittaciformes&quot;),
              (&quot;mammal&quot;, '2022-01',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;, '2022-02',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;,'2022-03',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;, '2022-04',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;,'2022-05',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;, '2022-06',&quot;Psittaciformes&quot;),
              (&quot;mammal&quot;, '2022-07',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;, '2022-08',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;, '2022-09',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;, '2022-10',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;, '2022-11',&quot;Falconiformes&quot;),
              (&quot;mammal&quot;, '2022-12',&quot;Falconiformes&quot;),
          
      
          ],
      
          columns=(&quot;animal&quot;, &quot;date&quot;, &quot;attribute&quot;),
      )
  </code></pre>
  <p>Now it's getting complicated. For each type of animal I want the count of the latest consecutive sequence of values within that group.</p>
  <p>The result should like</p>
  <pre><code>result = pd.DataFrame(
      [   (&quot;bird&quot;, 'Psittaciformes' ,4),
          (&quot;mammal&quot;, 'Falconiformes' ,6),
      ],
      columns=(&quot;animal&quot;, &quot;attribute&quot;, &quot;count&quot;),
  )
  result
  </code></pre>
  <p>I think it could be programmed with itergroup or something like that. What I'm looking for is a oneliner. It should be possible, is it?</p>