title: How to count the occurrence of strings in Dataframe that exists in another list of dicts?
body: <p>I have a Dataframe which look like this:</p>
  
  <pre><code>                   ngram
  --------------------------
    0                    []
    1   [_ting, tingk, ...]
    2   [_pend, pendi, ...]
    3   [_teat, teate, ...]
  ...                   ...
  999                    []
  </code></pre>
  
  <p>I also have a list of dicts which look like this:</p>
  
  <pre><code>[
    {
      "label": "Academic",
      "gram": "_ting"
    },
    {
      "label": "Facility",
      "gram": "_pend"
    },
    ....,
    {
      "label": "Others",
      "gram": "meing"
    },
  ]
  </code></pre>
  
  <p>How to count the occurrence of the words in the Dataframe, by checking whether if it exists in the list or not? My desire output is like this which will be used for the next computation:</p>
  
  <pre><code>academic_count = 1
  facility_count = 1
  services_count = 0
  others_count   = 0
  </code></pre>
  
  <p>I managed to implement it, but I'm just using nested looping, 
  which extremely slow because of the length of the Dataframe (1000 data) and the length of the list (4000+ data). This is my code:</p>
  
  <pre><code>df = pd.DataFrame()
  df['ngram'] = data
  
  academic_chance = []
  facility_chance = []
  services_chance = []
  others_chance   = []
  
  for idx, ngram_words in enumerate(df['ngram']):
      academic_probs = []
      facility_probs = []
      services_probs = []
      others_probs   = []
  
      for ngram in ngram_words:
          academic_count = 0
          facility_count = 0
          services_count = 0
          others_count   = 0
  
          for item in list_of_dicts:
              if ngram == item["gram"] and item["label"] == "Academic":
                  academic_count += 1
              elif ngram == item["gram"] and item["label"] == "Facility":
                  facility_count += 1
              elif ngram == item["gram"] and item["label"] == "Services":
                  services_count += 1
              elif ngram == item["gram"] and item["label"] == "Others":
                  others_count += 1
  
          academic_cond_prob = (academic_count + 1) / academic_denominator
          facility_cond_prob = (facility_count + 1) / facility_denominator
          services_cond_prob = (services_count + 1) / services_denominator
          others_cond_prob   = (others_count   + 1) / others_denominator
  
          academic_probs.append(academic_cond_prob)
          facility_probs.append(facility_cond_prob)
          services_probs.append(services_cond_prob)
          others_probs.append(others_cond_prob)
  
      academic_chance.append(np.prod(academic_probs) * academic_cat_probs)
      facility_chance.append(np.prod(facility_probs) * facility_cat_probs)
      services_chance.append(np.prod(services_probs) * services_cat_probs)
      others_chance.append(np.prod(others_probs)     * others_cat_probs)
  </code></pre>
  
  <p>Any ideas on how to make this more efficient?</p>