Wednesday, 24 April 2024

Pandas: Compute the sum of values within each group using mean method

Using ‘mean’ method of DataFrameGroupBy object, we can compute the mean of values within each group.

Example

data = { 'Name': ['Krishna', 'Chamu', 'Joel', 'Gopi', 'Sravya', "Raj"],
         'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Male', 'Female', 'Male'],
        'Weight': [74, 58, 85, 87, 63, 79]}

df = pd.DataFrame(data)

group_by_city = df.groupby('City')
mean_of_age_and_weight_group_by_city = group_by_city[['Age', 'Weight']].mean()

 

In the above example, I defined a DataFrame 'df' with columns "Name", "Age", "City", "Gender" and "Weight". Data is grouped by the 'City' column and the result is stored in the variable 'mean_of_age_and_weight_group_by_city'.

 

'group_by_city[['Age', 'Weight']].mean()' statement calculate the mean of Age and Weight of persons within each group.

 

Find the below working application.

 

mean_of_values_within_group.py

 

import pandas as pd

# Print the content of DataFrameGroupBy object
def print_group_by_result(group_by_object, label):
    print('*'*50)
    print(label,'\n')
    for group_name, group_data in group_by_object:
        print("Group Name:", group_name)
        print(group_data)
        print()
    print('*' * 50)


# Create a sample DataFrame
data = { 'Name': ['Krishna', 'Chamu', 'Joel', 'Gopi', 'Sravya', "Raj"],
         'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Male', 'Female', 'Male'],
         'Weight': [74, 58, 85, 87, 63, 79]}

df = pd.DataFrame(data)
print(df)

group_by_city = df.groupby('City')
print('\nGroup by city is')
print('type of group_by_city is : ', type(group_by_city))
print_group_by_result(group_by_city, 'Group by city details')

mean_of_age_and_weight_group_by_city = group_by_city[['Age', 'Weight']].mean()
print('\nmean_of_age_and_weight_group_by_city')
print(mean_of_age_and_weight_group_by_city)

Output

      Name  Age       City  Gender  Weight
0  Krishna   34  Bangalore    Male      74
1    Chamu   25    Chennai  Female      58
2     Joel   29  Hyderabad    Male      85
3     Gopi   41  Hyderabad    Male      87
4   Sravya   52  Bangalore  Female      63
5      Raj   23    Chennai    Male      79

Group by city is
type of group_by_city is :  <class 'pandas.core.groupby.generic.DataFrameGroupBy'>
**************************************************
Group by city details 

Group Name: Bangalore
      Name  Age       City  Gender  Weight
0  Krishna   34  Bangalore    Male      74
4   Sravya   52  Bangalore  Female      63

Group Name: Chennai
    Name  Age     City  Gender  Weight
1  Chamu   25  Chennai  Female      58
5    Raj   23  Chennai    Male      79

Group Name: Hyderabad
   Name  Age       City Gender  Weight
2  Joel   29  Hyderabad   Male      85
3  Gopi   41  Hyderabad   Male      87

**************************************************

mean_of_age_and_weight_group_by_city
            Age  Weight
City                   
Bangalore  43.0    68.5
Chennai    24.0    68.5
Hyderabad  35.0    86.0

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment