Home > Backend Development > Python Tutorial > How to Calculate the Average Time per Organization and Cluster in Pandas?

How to Calculate the Average Time per Organization and Cluster in Pandas?

Mary-Kate Olsen
Release: 2024-11-25 11:03:12
Original
576 people have browsed it

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())
Copy after login

2. Single-Level Grouping Approach (Cluster Only)

To directly calculate the mean of cluster groups, use:

df.groupby(['cluster']).mean()
Copy after login

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()
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template