


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:
-
Groupby ['cluster', 'org'] and Take Mean:
mean_by_cluster_org = df.groupby(['cluster', 'org'], as_index=False).mean()
Copy after login -
Groupby ['cluster'] and Calculate Average:
cluster_average = mean_by_cluster_org.groupby('cluster')['time'].mean()
Copy after login -
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()
Option 2: Groupby ['cluster', 'org'] and Use Mean:
cluster_org_mean = df.groupby(['cluster', 'org']).mean()
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
