I want to improve the time of a groupby in python pandas. I have this code:
groupby
df[\"Nbcontrats\"] = df.groupby([\'Client\', \'Month\'])[\'Contrat\
With the DataFrameGroupBy.size method:
DataFrameGroupBy.size
df.set_index(['Client', 'Month'], inplace=True) df['Nbcontrats'] = df.groupby(level=(0,1)).size() df.reset_index(inplace=True)
The most work goes into assigning the result back into a column of the source DataFrame.