Understand Pandas Groupby function in the most easiest way

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

For those who are familiar with oops concepts or are looking for a proper workflow, Pandas creates groups of DataFrame filtered by the unique values in the column specified and stores these data frames as key value pairs in an object and combines all the objects into a single object.

Read the above line once again after you go through this whole article.

Groupby works my doing three operations. Split, Apply, Combine. Let’s deep dive into how and what happens in each operation.

# Split

Let us consider a dataframe.

Let’s take the unique values in the city column

Now, Let’s filter the dataframe and store them as key value pairs

# Apply

let’s take any one dataframe from the groups

and apply the max() function

Let’s create a dictionary out of this

Let me write the whole process in a single cell

Let’s repeat the same for all cities

# Combine

let us combine all these three dataframe into a single dataframe

# Lets use groupby to do all of this in one line

# Additional

As I’ve written in the very beginning, groupby stores the filtered dataframes as objects with key value pairs.

--

--

Looking for my next opportunity to make change in a BIG way

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store