title: Group by method (in pandas) not working as expected
body: <p>I am trying to work with <a href="https://archive.org/details/stackexchange" rel="nofollow noreferrer">Stack Exchange data</a> (specifically, <a href="https://stats.stackexchange.com/">stats.stackexchange.com</a> questions).</p>
  <p>I am trying to find which were the <strong>top three tags for questions</strong> for all the years; and then construct a stacked barplot for the same.</p>
  <p>The example dataset that I am using below is not the actual one, however, resembles the original <em>stack exchange</em> data.</p>
  <p>The initial data frame looks like this:-</p>
  <p><a href="https://i.stack.imgur.com/vSXUK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vSXUK.png" alt="Original dataframe" /></a></p>
  <p>After some wrangling (code below):-</p>
  <pre><code>import re
  
  def my_function(x):
      
      output = []
      
      for s in re.findall(&quot;(?&lt;=\&lt;)(.*?)(?=\&gt;)&quot;, x):
          output.append(s)
          
      output2 = &quot;, &quot;.join(output)
      
      return output2
  
  
  df3['Tags'] = [my_function(x) for x in df3['Tags']]
  
  df3_new = df3.assign(Tags=df3['Tags'].str.split(',')).explode('Tags')
  </code></pre>
  <p>The new data frame looks something like this:-</p>
  <p><a href="https://i.stack.imgur.com/HFNCs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HFNCs.png" alt="New data frame" /></a></p>
  <p>Notice how the index is repeated? So, I decide to reset index.</p>
  <p><code>df3_new.reset_index(drop=True)</code></p>
  <p>and now the data frame looks like this:-</p>
  <p><a href="https://i.stack.imgur.com/Vma5p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vma5p.png" alt="enter image description here" /></a></p>
  <p>Finally, I use <strong>group by</strong> because I wish to obtain how many times a tag was repeated in any given year. I can then (later on) filter for the top three.</p>
  <pre><code>df3_groupby = df3_new.groupby(['Tags']).size().reset_index(name='Count')
  
  df3_groupby
  </code></pre>
  <p><a href="https://i.stack.imgur.com/tHIfc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tHIfc.png" alt="enter image description here" /></a></p>
  <p>Notice above how <em>response</em> and <em>thankful</em> are repeated? Let's try one more group by.</p>
  <pre><code>df3_groupby2 = df3_new.groupby(['Year', 'Tags'])['Year'].size().reset_index(name='Count')
  
  df3_groupby2
  </code></pre>
  <p><a href="https://i.stack.imgur.com/UNKS3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UNKS3.png" alt="enter image description here" /></a></p>
  <p>What about <strong>value_counts</strong>?</p>
  <p><a href="https://i.stack.imgur.com/hzi6A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hzi6A.png" alt="enter image description here" /></a></p>
  <p>I have tried some other things as well like, dropping the <em>Body</em> column, making a <em>set</em> for tags, however, nothing seems to be working.</p>
  <p>I would really appreciate help in either solving the above problem...or if there's a better way of knowing which question tags have the highest frequency in any given year?</p>