title: Conditional mean over a Pandas DataFrame
body: <p>I have a dataset from which I want a few averages of multiple variables I created.</p>
  
  <p>I started off with:</p>
  
  <pre><code>data2['socialIdeology2'].mean()
  
  data2['econIdeology'].mean()
  </code></pre>
  
  <p>^ that works perfectly, and gives me the averages I'm looking for.</p>
  
  <p>Now, I'm trying to do a conditional mean, so the mean only for a select group within the data set. (I want the ideologies broken down by whom voted for in the 2016 election) In Stata, the code would be similar to: <code>mean(variable) if voteChoice == 'Clinton'</code></p>
  
  <p>Looking into it, I came to the conclusion a conditional mean just isn't a thing (although hopefully I am wrong?), so I was writing my own function for it. </p>
  
  <p>This is me just starting out with a 'mean' function, to create a foundation for a conditional mean function:</p>
  
  <pre><code>def mean():
      sum = 0.0
      count = 0
      for index in range(0, len(data2['socialIdeology2'])):
          sum = sum + (data2['socialIdeology2'][index])
          print(data2['socialIdeology2'][index])
          count = count + 1
      return sum / count
  
  print(mean())
  </code></pre>
  
  <p>Yet I keep getting 'nan' as the result. Printing <code>data2['socialIdeology2'][index]</code> within the loop prints <code>nan</code> over and over again. </p>
  
  <p>So my question is: if the data stored within the <code>socialIdeology2</code> variable really is a <code>nan</code> (which I don't understand how it could be), why is it that the <code>.mean()</code> function works with it?</p>
  
  <p>And how can I get generate means by category?</p>