Site icon Machine Learning Projects

Python Program to use groupby – 2022

Machine Learning Projects

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

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.

Extract a full group

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

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

Check out our other python programming examples

Exit mobile version