Using
‘sum’ method of DataFrameGroupBy object, we can compute the sum 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')
sum_of_age_and_weight_group_by_city = group_by_city[['Age', 'Weight']].sum()
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 'sum_of_age_and_weight_group_by_city'.
'group_by_city[['Age', 'Weight']].sum()' statement calculate the sum of Age and Weight of persons within each group.
Find the below working application.
sum_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')
sum_of_age_and_weight_group_by_city = group_by_city[['Age', 'Weight']].sum()
print('\nsum_of_age_and_weight_group_by_city')
print(sum_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 ************************************************** sum_of_age_and_weight_group_by_city Age Weight City Bangalore 86 137 Chennai 48 137 Hyderabad 70 172
No comments:
Post a Comment