Count by unique pair of columns in pandas

Given a pandas dataframe, we have to count by unique pair of columns. By Pranit Sharma Last updated : October 06, 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.

Problem statement

Suppose we are given the dataframe containing two columns each of which has repeating values, we need to figure out how to count by the number of rows for unique pair of columns.

Counting by unique pair of columns

For this purpose, we will use groupby and apply the size() method on the group by an object.

The groupby() is a simple but very useful concept in pandas. By using groupby, we can create grouping of certain values and perform some operations on those values.

The groupby() method splits the object, applies some operations, and then combines them to create a group hence large amounts of data and computations can be performed on these groups.

Let us understand with the help of an example,

Python program to count by unique pair of columns in pandas

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
    'id': ['192', '192', '168', '168'],
    'user': ['a', 'a', 'b', 'b']

# Creating a DataFrame
df = pd.DataFrame(d)

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

# Getting result by unique pair of columns
res = df.groupby(['id', 'user']).size()

# Display result


The output of the above program is:

Example: Count by unique pair of columns in pandas

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.