title: Pandas dataframe: Count occurrence of list element in dataframe rows
body: <p>Using python, I would like to count the occurrence of a lists elements for each row in a dataframe, and aggregate each elements occurrence.</p>
  
  <p>Here is the dataframe I am working with: </p>
  
  <pre><code>#Cluster_number_1   Cluster Type:   terpene
  #Cluster_number_2   Cluster Type:   nrps
  #Cluster_number_3   Cluster Type:   terpene
  #Cluster_number_4   Cluster Type:   nrps
  #Cluster_number_5   Cluster Type:   nrps
  #Cluster_number_6   Cluster Type:   nrps
  #Cluster_number_7   Cluster Type:   t1pks
  #Cluster_number_8   Cluster Type:   other
  #Cluster_number_9   Cluster Type:   t1pks
  #Cluster_number_10  Cluster Type:   nrps
  </code></pre>
  
  <p>The corresponding list: </p>
  
  <pre><code>cluster_type = ["t1pks", "nrps", "terpene", "other"]
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>BGC_Class    Count
  t1pks            2
  nrps             5
  terpene          2
  other            1
  </code></pre>
  
  <p>To help explain, borrowing from unix $ variables:</p>
  
  <pre><code>file = "cluster_counts.txt"
  cluster_count = open(file, "w")
  
  cluster_count.write(+$1+"\t"+$2"\n")
  </code></pre>
  
  <p>Where $1 is the first element in the list, and $2 is the number of times it occurs, across all rows. </p>
  
  <p>The dataframes won't exceed 100 lines, so efficiency is no issue. </p>
  
  <p>Best, B.D. </p>
  
  <p>I found something to get me started here <a href="https://stackoverflow.com/questions/2600191/how-to-count-the-occurrences-of-a-list-item">How to count the occurrences of a list item?</a>. </p>
  
  <pre><code>&gt;&gt;&gt; l = ["a","b","b"]
  &gt;&gt;&gt; [[x,l.count(x)] for x in set(l)]
  [['a', 1], ['b', 2]]
  </code></pre>
  
  <p>However this only counts the occurrences of elements within the list containing it. </p>
  
  <p>I don't know how to count the occurrence of my lists elements in the dataframe. </p>