Get Sum by Group in Python – DataFrame Subgroups (2 Examples)
This tutorial explains how to calculate the sum by group in the Python programming language.
Setting up the Examples
import pandas as pd # Import pandas library in Python |
import pandas as pd # Import pandas library in Python
my_df = pd.DataFrame({'A':range(19, 28), # Constructing a pandas DataFrame 'B':[6, 7, 3, 9, 1, 3, 8, 8, 9], 'C':range(20, 11, - 1), 'GRP_a':['gr1', 'gr1', 'gr2', 'gr3', 'gr1', 'gr2', 'gr2', 'gr3', 'gr3'], 'GRP_b':['a', 'b', 'c', 'c', 'a', 'b', 'b', 'a', 'a']}) print(my_df) # A B C GRP_a GRP_b # 0 19 6 20 gr1 a # 1 20 7 19 gr1 b # 2 21 3 18 gr2 c # 3 22 9 17 gr3 c # 4 23 1 16 gr1 a # 5 24 3 15 gr2 b # 6 25 8 14 gr2 b # 7 26 8 13 gr3 a # 8 27 9 12 gr3 a |
my_df = pd.DataFrame({'A':range(19, 28), # Constructing a pandas DataFrame 'B':[6, 7, 3, 9, 1, 3, 8, 8, 9], 'C':range(20, 11, - 1), 'GRP_a':['gr1', 'gr1', 'gr2', 'gr3', 'gr1', 'gr2', 'gr2', 'gr3', 'gr3'], 'GRP_b':['a', 'b', 'c', 'c', 'a', 'b', 'b', 'a', 'a']}) print(my_df) # A B C GRP_a GRP_b # 0 19 6 20 gr1 a # 1 20 7 19 gr1 b # 2 21 3 18 gr2 c # 3 22 9 17 gr3 c # 4 23 1 16 gr1 a # 5 24 3 15 gr2 b # 6 25 8 14 gr2 b # 7 26 8 13 gr3 a # 8 27 9 12 gr3 a
Example 1: Calculating Sum by Group in Python
print(my_df.groupby('GRP_a').sum()) # Computing the column sum by group # A B C # GRP_a # gr1 62 14 55 # gr2 70 14 47 # gr3 75 26 42 |
print(my_df.groupby('GRP_a').sum()) # Computing the column sum by group # A B C # GRP_a # gr1 62 14 55 # gr2 70 14 47 # gr3 75 26 42
Example 2: Calculating Sum by Group & Subgroup in Python
print(my_df.groupby(['GRP_a', 'GRP_b']).sum()) # Computing the column sum by multiple groups # A B C # GRP_a GRP_b # gr1 a 42 7 36 # b 20 7 19 # gr2 b 49 11 29 # c 21 3 18 # gr3 a 53 17 25 # c 22 9 17 |
print(my_df.groupby(['GRP_a', 'GRP_b']).sum()) # Computing the column sum by multiple groups # A B C # GRP_a GRP_b # gr1 a 42 7 36 # b 20 7 19 # gr2 b 49 11 29 # c 21 3 18 # gr3 a 53 17 25 # c 22 9 17