Count the number of Occurrence of Values based on another column
<p>I have a question regarding creating pandas dataframe according to the sum of other column.</p>

<p>For example, I have this dataframe</p>

<pre><code> Country    |    Accident
 England           Car
 England           Car
 England           Car
  USA              Car
  USA              Bike
  USA              Plane
 Germany           Car
 Thailand          Plane
</code></pre>

<p>I want to make another dataframe based on the sum value of all accident  based on the country. We will disregard the type of the accident, while summing them all based on the country.</p>

<p>My desire dataframe would look like this</p>

<pre><code>  Country    |    Sum of Accidents
  England              3
    USA                3
  Germany              1
  Thailand             1
</code></pre>

<p><strong><em>Option 1</em></strong><br>
Use <code>value_counts</code></p>

<pre><code>df.Country.value_counts().reset_index(name='Sum of Accidents')
</code></pre>

<p><a href="http://i.stack.imgur.com/G0Gii.png"><img src="http://i.stack.imgur.com/G0Gii.png" alt="enter image description here"></a></p>

<p><strong><em>Option 2</em></strong><br>
Use <code>groupby</code> then <code>size</code></p>

<pre><code>df.groupby('Country').size().sort_values(ascending=False) \
  .reset_index(name='Sum of Accidents')
</code></pre>

<p><a href="http://i.stack.imgur.com/K7RDt.png"><img src="http://i.stack.imgur.com/K7RDt.png" alt="enter image description here"></a></p>

python pandas