Python Program to use groupby – 2024

Hey guys, in this blog we will see Python Programs to use groupby. We will see how to groupby using one or more columns.

So without any further due, let’s do it…

Syntax: DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, **kwargs)

Returns: GroupBy object

Reading the Dataframe

import pandas as pd

# Read the dataframe
df = pd.read_csv('nba.csv')

# Let's see the dataframe
  • Here we are simply reading our ‘nba.csv’ dataset using pd.read_csv() method.
  • Download this file from here.
image 30

Example 1: Groupby one column

# applying groupby() function to group the data on team value.
team = df.groupby('Team')

# Let's print the first entries in all the groups formed.
  • In this step we are using dataframe.groupby(column) to group our dataframe.
  • And then we are using group.first() to print the first entries of all the groups which are formed from our dataframe.
image 31

Extract a full group

# Finding the values contained in the "Brooklyn Nets" group
team.get_group('Brooklyn Nets')
image 32

Example 2: Groupby multiple columns

# importing pandas as pd
import pandas as pd

# Creating the dataframe
df = pd.read_csv("nba.csv")

# First grouping based on "Team"
# Within each team we are grouping based on "Position"
team_pos = df.groupby(['Team', 'Position'])

# Print the first value in each group
  • We can also create groups using multiple columns.
  • Here in this example, we used 2 columns [‘Team’, ‘Position’] to create multilevel index-type groups.
image 33

Check out our other python programming examples

Leave a Reply

Your email address will not be published. Required fields are marked *