top of page
Writer's pictureSagnik K

The groupby Method in Python

Let's say that you are making a Python project with the usual imports: NumPY and Pandas. Now let's say that the project that you are making is going to need a way to group all your rows(from a table) all together. Well you can then use the groupby feature! This blog is about how to use the groupby feature. If you have any questions, read the questions section. If not, you can go on to the coding section. Let's crack on with the next section now!


QUESTIONS:

  1. What is the groupby feature?

  2. Why is the groupby method used

Answer to Q1: The groupby feature allows you to group rows together based off of a column and perform a specific function on them.

Answer to Q2: We will need to use the groupby feature in order to group some rows, from a table, to then perform a specific function on them.


Let's move on to the coding section now!


CODING:

(Just a reminder, if you want to follow along with me, feel free to do so. I would recommend Kaggle or Google Colab.)

1. Always Import, Or Else(Import the libraries)...

First, we will have to import all needed libraries(as always).

import numpy as np
import pandas as pd

2. DataFrames and Phone Companies(Creating a DataFrame)

We will now create a DataFrame to use the groupby feature on. The code is down below:


df = pd.DataFrame([ ['Apple', 'Steve', 200],

                    ['Apple', 'Tim', 120],
                    
                    ['Android', 'Andy', 125],
                    
                    ['Android', 'Jamie', 250],
                    
                    ['Huawei', 'Dean', 150],
                    
                    ['Huawei', 'Andrea', 500]])
                    
df.columns = ['Company', 'Employee Name', 'Phone Sales']

df

This is the output that you should have gotten:

Moving on to the next step!

3. Objects for the groupby Method(Creating a groupby Object)!

We will now create a groupby object. The reason why we are is because with this, we can look at some data more closely. The code is down below:

df.groupby('Organization')

If you run this, you should have gotten an output that looks like this:

If you see that output, then you would know that you successfully made a groupby object.


Let's move on with the next step!

4. The Mean, Sum, and the Standard Deviation(Finding the Mean, Sum and the Standard Deviation of the Company)

We will now find the mean, the sum, and the standard deviation. The code is down below:

 df.groupby('Company').mean()
 #The mean (or average) of the sales column
 df.groupby('Company').sum()
 #The sum of the sales column
 df.groupby('Company').std()
 #The standard deviation of the sales column

The results after running are:

The FINAL Step is the Describe Method!(Using the Describe Method Along with groupby)

Now, for the final step, we are going to use the describe method with the groupby feature. Here are the lines of code that you will need:

df.groupby('Company').describe()

The output is down below:

CONCLUSION:

And with that, this blog will end. I hope you like this blog and without further ado, happy reading!

334 views
bottom of page