getting percentage of values meeting conditions within a Pandas group by
<p>I have a dataframe of of <code>People</code>, <code>Days</code>, and <code>Types</code>. The data doesn't really make sense, it's just an example.</p>

<p>I'd like to do a group by first on <code>People</code> then <code>Type</code> and then find the percentage of Days that are less than or equal to 3.</p>

<p>In order to do this, I am created a <code>Boolean</code> column for equal or under 3 days. Then applying a <code>count</code> and <code>sum</code> aggregate. I'm not a big fan of this method because I really only need the <code>count</code> for the <code>Days</code> column and <code>sum</code> for the <code>Under Day Limit</code> column. This method is essentially creating two unnecessary columns and creates a number of extra steps. How can I clean this code up so it runs more efficiently over my larger dataset.</p>

<pre><code>import pandas as pd
# create dataframe
df = pd.DataFrame(data=[['A', 4, 'Type 1'],
                        ['A', 1, 'Type 1'],
                        ['A', 3, 'Type 2'],
                        ['A', 0, 'Type 1'],
                        ['A', 12, 'Type 2'],
                        ['B', 1, 'Type 1'],
                        ['B', 3, 'Type 1'],
                        ['B', 5, 'Type 2']],
                   columns=['Person', 'Days', 'Type'])

df['Under Day Limit'] = df['Days'] &lt;= 3;
print df
df = df.groupby(['Person', 'Type']).agg(['count', 'sum'])

df['Percent under Day Limit'] = df['Under Day Limit']['sum'] / df['Days']['count']
print df
</code></pre>

<p>Ouput:</p>

<pre><code>               Days     Under Day Limit     Percent under Day Limit
              count sum           count sum                        
Person Type                                                        
A      Type 1     3   5               3   2                0.666667
       Type 2     2  15               2   1                0.500000
B      Type 1     2   4               2   2                1.000000
       Type 2     1   5               1   0                0.000000
</code></pre>