Groupby sum in pandas dataframe python

Groupby sum in pandas python can be accomplished by groupby() function. Groupby sum of multiple column and single column in pandas is accomplished by multiple ways some among them are groupby() function and aggregate() function.  let’s see how to

  • Groupby single column in pandas – groupby sum
  • Groupby multiple columns in groupby sum
  • Groupby sum using aggregate() function
  • Groupby sum using pivot() function.
  • using reset_index() function for groupby multiple columns and single column

Generic Groupby sum 1

First let’s create a dataframe

import pandas as pd
import numpy as np

data = {'Product':['Box','Bottles','Pen','Markers','Bottles','Pen','Markers','Bottles','Box','Markers','Markers','Pen'],
       'State':['Alaska','California','Texas','North Carolina','California','Texas','Alaska','Texas','North Carolina','Alaska','California','Texas'],
       'Sales':[14,24,31,12,13,7,9,31,18,16,18,14]}

df1=pd.DataFrame(data, columns=['Product','State','Sales'])

df1

df1 will be

Groupby count in pandas python 1

 

 

Groupby single column – groupby sum pandas python:

groupby() function takes up the column name as argument followed by sum() function as shown below


''' Groupby single column in pandas python'''
df1.groupby(['State'])['Sales'].sum()

We will groupby sum with single column (State), so the result will be

Groupby count in pandas python 3

using  reset_index()

reset_index() function resets and provides the new index to the grouped by dataframe and makes them a proper dataframe structure


''' Groupby single column in pandas python using reset_index()'''

df1.groupby(['State'])['Sales'].sum().reset_index()

We will groupby sum with “State” column along with the reset_index() will give a proper table structure , so the result will be

Groupby count in pandas python 4

 

 

Groupby multiple columns – groupby sum python:


''' Groupby multiple columns in pandas python'''

df1.groupby(['State','Product'])['Sales'].sum()

We will groupby sum with State and Product columns, so the result will be

Groupby count in pandas python 5

 

Groupby Sum of multiple columns in pandas using  reset_index():

reset_index() function resets and provides the new index to the grouped by dataframe and makes them a proper dataframe structure


''' Groupby multiple columns in pandas python using reset_index()'''

df1.groupby(['State','Product'])['Sales'].sum().reset_index()

We will groupby sum with “Product” and “State” columns along with the reset_index() will give a proper table structure , so the result will be

Groupby count in pandas python 6

 

 

Using aggregate() function:

agg() function takes ‘sum’ as input which performs groupby sum, reset_index() assigns the new index to the grouped by dataframe and makes them a proper dataframe structure


''' Groupby multiple columns in pandas python using agg()'''

df1.groupby(['State','Product'])['Sales'].agg('sum').reset_index()

We will compute groupby sum using agg() function with “Product” and “State” columns along with the reset_index() will give a proper table structure , so the result will be

Groupby count in pandas python 6

 

using Pivot() function :

You can use the pivot() functionality to arrange the data in a nice table.


''' Groupby multiple columns in pandas python using pivot()'''

df1.groupby(['State','Product'],as_index = False).sum().pivot('State','Product').fillna(0)

groupby() function along with the pivot function() gives a nice table format as shown below

Groupby count in pandas python 7

 

p Group by sum in pandas dataframe python                                                                                                     n Group by sum in pandas dataframe python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts