Home > Article > Backend Development > How to Calculate the Average Time per Organization and Cluster in Pandas?
Group-by Calculation: Average of Time per Organization and Cluster
In Pandas, calculating the average of time per organization within each cluster can be achieved using the groupby() function.
1. Nested Grouping Approach
To calculate the average first by ['cluster', 'org'] and then by 'cluster', use the following code:
(df.groupby(['cluster', 'org'], as_index=False).mean() .groupby('cluster')['time'].mean())
2. Single-Level Grouping Approach (Cluster Only)
To directly calculate the mean of cluster groups, use:
df.groupby(['cluster']).mean()
3. Grouping by ['cluster', 'org'] and Applying Mean
An alternative approach is to group by ['cluster', 'org'] and then apply the mean function:
df.groupby(['cluster', 'org']).mean()
Result
The expected result for all approaches is:
cluster | mean(time) |
---|---|
1 | 15 |
2 | 54 |
3 | 6 |
The above is the detailed content of How to Calculate the Average Time per Organization and Cluster in Pandas?. For more information, please follow other related articles on the PHP Chinese website!