Groupby in python pandas: Fast Way
<p>I want to improve the time of a <code>groupby</code> in python pandas. 
I have this code:</p>

<pre><code>df["Nbcontrats"] = df.groupby(['Client', 'Month'])['Contrat'].transform(len)
</code></pre>

<p>The objective is to count how many contracts a client has in a month and add this information in a new column (<code>Nbcontrats</code>).</p>

<ul>
<li><code>Client</code>: client code</li>
<li><code>Month</code>: month of data extraction</li>
<li><code>Contrat</code>: contract number</li>
</ul>

<p>I want to improve the time. Below I am only working with a subset of my real data:</p>

<pre><code>%timeit df["Nbcontrats"] = df.groupby(['Client', 'Month'])['Contrat'].transform(len)
1 loops, best of 3: 391 ms per loop

df.shape
Out[309]: (7464, 61)
</code></pre>

<p>How can I improve the execution time?</p>