Python Pandas : group by in group by and average? Python Pandas : group by in group by and average? pandas pandas

Python Pandas : group by in group by and average?


If you want to first take mean on the combination of ['cluster', 'org'] and then take mean on cluster groups, you can use:

In [59]: (df.groupby(['cluster', 'org'], as_index=False).mean()            .groupby('cluster')['time'].mean())Out[59]:cluster1          152          543           6Name: time, dtype: int64

If you want the mean of cluster groups only, then you can use:

In [58]: df.groupby(['cluster']).mean()Out[58]:              timecluster1        12.3333332        54.0000003         6.000000

You can also use groupby on ['cluster', 'org'] and then use mean():

In [57]: df.groupby(['cluster', 'org']).mean()Out[57]:               timecluster org1       a    438886        c        232       d      9874        h        343       w         6


I would simply do this, which literally follows what your desired logic was:

df.groupby(['org']).mean().groupby(['cluster']).mean()