Home > Backend Development > Python Tutorial > How to Calculate the Average 'Time' per 'Org' per 'Cluster' and Then the Average 'Time' for Each 'Cluster' Group in Pandas?

How to Calculate the Average 'Time' per 'Org' per 'Cluster' and Then the Average 'Time' for Each 'Cluster' Group in Pandas?

DDD
Release: 2024-11-16 22:54:03
Original
602 people have browsed it

How to Calculate the Average 'Time' per 'Org' per 'Cluster' and Then the Average 'Time' for Each 'Cluster' Group in Pandas?

Groupby Groupby and Average in Pandas

Problem:

Given a DataFrame with 'cluster', 'org', and 'time' columns, how can you calculate the average 'time' per 'org' per 'cluster' and then take the average of 'time' for each 'cluster' group?

Expectation:

cluster mean(time)
1 15
2 54
3 6

Solution:

To achieve the desired result, you can use the following steps:

  1. Groupby ['cluster', 'org'] and Take Mean:

    mean_by_cluster_org = df.groupby(['cluster', 'org'], as_index=False).mean()
    Copy after login
  2. Groupby ['cluster'] and Calculate Average:

    cluster_average = mean_by_cluster_org.groupby('cluster')['time'].mean()
    Copy after login
  3. Display Results:

    print(cluster_average)
    Copy after login

Alternatively, you can also use the following methods to tackle this problem:

Option 1: Groupby Only ['cluster'] and Take Mean:

cluster_only_average = df.groupby('cluster').mean()
Copy after login

Option 2: Groupby ['cluster', 'org'] and Use Mean:

cluster_org_mean = df.groupby(['cluster', 'org']).mean()
Copy after login

Regardless of the approach you choose, the output will provide you with the average 'time' per 'org' for each 'cluster' group and the overall average of 'time' per 'cluster'.

The above is the detailed content of How to Calculate the Average 'Time' per 'Org' per 'Cluster' and Then the Average 'Time' for Each 'Cluster' Group 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template