Counting occurrence of a gender in a dataframe by age
<p>I have a dataframe like</p>

<pre><code>age gender
69  M
29  F
61  F
66  F
52  M
</code></pre>

<p>What I would like to know is how many males and females there are in each age group.  Is there somehow I could use <code>groupby</code> to group the data by age and then use <code>agg</code> to count the instances of male and female?</p>

<p>you can groupby age AND gender:</p>

<pre><code>df.groupby(['age','gender']).size()
df.reset_index(inplace=True)
</code></pre>

<p>will give</p>

<pre><code>age  gender
69   M       1
29   F       1
61   F       1
66   F       1
52   M       1
</code></pre>

python pandas