In this post, we will learn How to groupby sum mutiple column Pandas python. The groupby() function is used to group the same repeated values in given data and split the dataframe into different groups. This function returns a Dataframegroupby object.We can apply aggregate() and functions like sum() and mean(), max(), and count(), min(),median() on grouped dataframe.We can apply single or multiple functions on grouped dataframe by passing these functions as an argument to function agg() by e.g ‘sum’ or a list of functions like [‘sum’,’max’,’min’].
1. How to groupby sum mutiple column pandas using sum()
In this example, The pandas.groupby() function is used to group the same values and split the dataframe into groups. In this example, we have a group dataframe by multiple columns [‘Name’,’ Marks’] and apply the sum to get the SUM of multiple columns. In another way, we have reset the index and renamed the column name using the below steps.
- to reset the index name on grouped dataframe.We have used reset_index() along with rename() function
- The rename() function is used to rename columns and reset the index name on grouped dataframe.
import pandas as pd
data = {
'Name': ['Rama', 'Rama', 'Max', 'Rama'],
'Marks':[97,97,100,97],
'Fee':[100,100,300,100],
'Tution_Fee':[400,400,600,400]
}
dfobj = pd.DataFrame(data)
dfobj = dfobj.groupby(['Name','Marks'])['Fee', 'Tution_Fee'].sum()
print(dfobj)
#To rename the column by group by use this code
dfobj2 = dfobj.groupby(['Name','Marks'])['Fee', 'Tution_Fee'].sum().reset_index().rename(columns={'Fee':'Fee_Sum','Tution_Fee': 'Tution_Feesum'})
print('rename columns:',dfobj2)
Output
Fee Tution_Fee
Name Marks
Max 100 300 600
Rama 97 300 1200
rename columns :
Name Marks Fee_Sum Tution_Feesum
0 Max 100 300 600
1 Rama 97 300 1200
2. How to groupby sum mutiple column pandas using agg()
The Pandas agg() method is used to apply single or multiple aggregate functions on the Pandas dataframe. We can pass a single function to agg() function like ‘Sum’ or a list of multiple parameters to the aggregate function. In this below example, We have passed a single aggregate function sum() as a parameter to agg() function to get the sum of mutiple columns.
- To reset the index name on grouped dataframe.We have used reset_index() along with rename() function
- The rename() function is used to rename columns and reset the index name on grouped dataframe.
import pandas as pd
data = {
'Name': ['Rama', 'Rama', 'Max', 'Rama'],
'Marks':[97,97,100,97],
'Fee':[100,100,300,100],
'Tution_Fee':[400,400,600,400]
}
dfobj = pd.DataFrame(data)
dfobj = dfobj.groupby(['Name','Marks']).agg({'Fee':'sum', 'Tution_Fee': 'sum'})
print(dfobj)
#rename the columsn after apply sum()
dfobj2 = dfobj.groupby(['Name','Marks']).agg({'Fee':'sum', 'Tution_Fee': 'sum'}).reset_index().rename(columns={'Fee':'Sum_Fee','Tution_Fee': 'Sum_TutionFee'})
print(dfobj2)
Output
Fee Tution_Fee
Name Marks
Max 100 300 600
Rama 97 300 1200
Name Marks Sum_Fee Sum_TutionFee
0 Max 100 300 600
1 Rama 97 300 1200
- Pandas sum rows by columns(6 ways)
- Pandas sum columns by multiple conditions
- How to Pandas sum all columns except one
- How to sum columns with Nan Pandas Dataframe
- How to sum specific rows Pandas dataframe
- How to Sum rows by condition Pandas Dataframe
- How to sum Pandas columns into new column
3. How to groupby sum Mutiple columns Pandas using pivot
In this example, we have grouped mutiple columns in Pandas by using the pivot() function. It returns the reshaped data organized by index/column values. Let us understand with the below example.
import pandas as pd
data = {
'Name': ['Rama', 'Rama', 'Max', 'Rama'],
'Marks':[97,97,100,97],
'Fee':[100,100,300,100],
'Tution_Fee':[400,400,600,400]
}
dfobj = pd.DataFrame(data)
dfobj = dfobj.groupby(['Name'],as_index = False).sum().pivot(index='Name',columns= ['Fee','Tution_Fee']).fillna(0)
print(dfobj)
Output
Marks
Fee 300
Tution_Fee 600 1200
Name
Max 100.0 0.0
Rama 0.0 291.0
4. Pandas groupby sum mutiple columns and count
In this example, we will learn how to group by mutiple columns sum and count in Pandas dataframe. First, we have to group the entire column by column ‘Name’ and find the ‘count’ and ‘Sum’ of columns.
- The agg() function is used to count the Marks column and Sum of the ‘Tution_Fee’ column.
- The next step is to reset the index using the reset_index() function of the dataframe and
- rename the column using rename() function ‘Marks’ to ‘Marks count’
import pandas as pd
data = {
'Name': ['Rama', 'Rama', 'Max', 'Rama'],
'Marks':[97,97,100,97],
'Fee':[100,100,300,100],
'Tution_Fee':[400,400,600,400]
}
dfobj = pd.DataFrame(data)
dfobj = dfobj.groupby('Name').agg({'Marks':'count', 'Tution_Fee': 'sum'}).reset_index().rename(columns={'Marks':'Marks count'})
print(dfobj)
Output
Name Marks count Tution_Fee
0 Max 1 600
1 Rama 3 1200
Summary
In this post, we have learned How to groupby sum mutiple column Pandas using the different functions of the Pandas library that includes groupby(), Sum() and agg(), and pivot() with examples.