Pandas aggregate count distinct

Learn about the Python Pandas aggregate count distinct. By Pranit Sharma Last updated : September 23, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

pandas.DataFrame.groupby() Method

The groupby() is a simple but very useful concept in Pandas. By using groupby, we can create a grouping of certain values and perform some operations on those values. The groupby() method split the object, apply some operations, and then combines them to create a group hence a large amount of data and computations can be performed on these groups.

To use an aggregate function in pandas groupby, we will use agg() method of groupby(), which will allow us to perform certain operations.

In programming, aggregate functions are those functions that return a scalar value after applying some operations like count, sum, avg, etc.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

Python code for Pandas aggregate count distinct

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a DataFrame
df = pd.DataFrame({
    'Match Number':[2,7,13,17,21],

# Display DataFrame
print("Created DataFrame:\n",df,"\n")

# Using groupby and agg
df = df.groupby('Match Number').agg({'Score': np.sum, 'Player': pd.Series.nunique})

# Display final result
print("Aggregate result:\n",df)


The output of the above program is:

Example: Aggregate count distinct

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.