How to use groupby in pandas to calculate a percentage / proportion total based on a criteria in another column
<p>I'm trying to work out how to use the <code>groupby</code> function in pandas to work out the proportions of values per year with a given Yes/No criteria.</p>

<p>For example, I have a dataframe called <code>names</code>:</p>

<pre><code>  Name  Number  Year   Sex Criteria
0  name1     789  1998  Male      N
1  name1     688  1999  Male      N
2  name1     639  2000  Male      N
3  name2     551  1998  Male      Y
4  name2     499  1999  Male      Y
</code></pre>

<p>I can use</p>

<pre><code>namesgrouped = names.groupby(["Sex", "Year", "Criteria"]).sum()
</code></pre>

<p>to get: </p>

<pre><code>                   Number
Sex    Year      Criteria
Male   1998 N        14507
            Y         2308
       1999 N        14119
            Y         2331
</code></pre>

<p>and so on.  I would like the 'Number Criteria' column to show the % of the total for each gender and year - so instead of N = 14507 and Y = 2308 for 1998 above I'd have N = 86.27% and Y = 13.73%.</p>

<p>Can anyone advise how to do this?</p>