title: How to get the fraction of occurrences of a certain value in a Pandas Series?
body: <p>Suppose I have a <code>DataFrame</code> containing a column <code>A</code> which contains only values <code>'foo'</code> and <code>'bar'</code>, and I'd like to compute the fraction of <code>foo</code>s.  One way to do this is by using Boolean selection together with the <code>__len__</code> function:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                            'foo', 'bar', 'foo', 'foo'],
                     'B' : ['one', 'one', 'two', 'three',
                            'two', 'two', 'one', 'three'],
                     'C' : np.random.randn(8),
                     'D' : np.random.randn(8)})
  
  assert len(df[df['A'] == 'foo']) / len(df) == 0.625
  </code></pre>
  
  <p>However, I feel that this can also be done in a more <code>pandas</code>-native way using <code>groupby</code> and <code>count</code>. For example,</p>
  
  <pre><code>print(df.groupby('A').count() / len(df))
  </code></pre>
  
  <p>yields</p>
  
  <pre><code>         B      C      D
  A                       
  bar  0.375  0.375  0.375
  foo  0.625  0.625  0.625
  </code></pre>
  
  <p>What is the recommended way to proceed? Do I just select the at <code>foo</code> in any column?</p>