title: How to let pandas groupby add a count column for each group after applying list aggregations?
body: <p>I have a pandas DataFrame:</p>
  <pre><code>df = pd.DataFrame({&quot;col_1&quot;: [&quot;apple&quot;, &quot;banana&quot;, &quot;apple&quot;, &quot;banana&quot;, &quot;banana&quot;],
                     &quot;col_2&quot;: [1, 4, 8, 8, 6],
                     &quot;col_3&quot;: [56, 4, 22, 1, 5]})
  </code></pre>
  <p>on which I apply a groupby operation that aggregates multiple columns into a list, using:</p>
  <pre><code>df = df.groupby(['col_1'])[[&quot;col_2&quot;, &quot;col_3&quot;]].agg(list)
  </code></pre>
  <p>Now I want to additionally add a column that for each resulting group adds the number of elements in that group. The result should look like this:</p>
  <pre><code>{&quot;col_1&quot;: [&quot;apple&quot;, &quot;banana&quot;],
   &quot;col_2&quot;: [[1, 8], [4, 8, 6]],
   &quot;col_3&quot;: [[56, 22], [4, 1, 5]]
   &quot;count&quot;: [2, 3]}
  </code></pre>
  <p>I tried the following from reading other Stack Overflow posts:</p>
  <pre><code>df = df.groupby(['col_1'])[[&quot;col_2&quot;, &quot;col_3&quot;, &quot;col_4&quot;]].agg(list).size()
  
  df = df.groupby(['col_1'])[[&quot;col_2&quot;, &quot;col_3&quot;, &quot;col_4&quot;]].agg(list, &quot;count&quot;)
  
  df = df.groupby(['col_1'])[[&quot;col_2&quot;, &quot;col_3&quot;, &quot;col_4&quot;]].agg(list).agg(&quot;count&quot;)
  </code></pre>
  <p>But all gave either incorrect results (option 3) or an error (option 1 and 2)</p>
  <p>How to solve this?</p>