Groupby Value Counts on Pandas DataFrame: Optimized Methods for Large Datasets

Groupby Value Counts on Pandas DataFrame

=====================================================

In this article, we will explore how to group a pandas DataFrame by multiple columns and count the number of unique values in each group. We’ll cover the different approaches available, including using groupby with size, as well as some performance optimization techniques.

Introduction


The pandas library is one of the most popular data analysis libraries for Python, providing efficient data structures and operations for data manipulation and analysis. One common operation in data analysis is grouping data by multiple columns and counting the number of unique values in each group. In this article, we’ll cover how to achieve this using pandas.

Grouping Data with Pandas


The groupby function in pandas allows us to group a DataFrame by one or more columns and perform various operations on each group. We can use size to count the number of rows in each group.

Using groupby with size

Here’s an example of how to group a DataFrame by multiple columns using groupby and size:

import pandas as pd
import numpy as np

# Create a sample DataFrame
df = pd.DataFrame({
    'id': np.random.choice(100, 1000000),
    'group': np.random.choice(20, 1000000),
    'term': np.random.choice(10, 1000000)
})

# Group by id and group, count the number of unique values in each group
result = df.groupby(['id', 'group', 'term']).size().unstack(fill_value=0)

print(result)

This will output a DataFrame with the same index as the original df, but with an additional dimension representing the unique values in each group.

Timing and Performance Optimization


When working with large datasets, performance can be a critical factor. In this section, we’ll discuss some techniques for optimizing the performance of our code.

Using chunking

One approach to improve performance is to use chunking. Instead of loading the entire DataFrame into memory at once, we can split it into smaller chunks and process each chunk separately.

import pandas as pd
import numpy as np

# Create a sample DataFrame with 1 million rows
df = pd.DataFrame({
    'id': np.random.choice(100, 1000000),
    'group': np.random.choice(20, 1000000),
    'term': np.random.choice(10, 1000000)
})

# Define the chunk size
chunk_size = 10000

# Loop over each chunk and process it
for i in range(0, len(df), chunk_size):
    chunk = df.iloc[i:i + chunk_size]
    result = chunk.groupby(['id', 'group', 'term']).size().unstack(fill_value=0)
    # Append the result to a list
    results.append(result)

# Concatenate all chunks and print the result
print(pd.concat(results))

By using chunking, we can reduce the amount of memory required to process large datasets.

Using dask

Another approach is to use the dask library, which provides parallelized versions of pandas operations. We can create a dask DataFrame from our original DataFrame and then use groupby and size to perform the operation in parallel.

import pandas as pd
import numpy as np
from dask.dataframe import from_pandas

# Create a sample DataFrame with 1 million rows
df = pd.DataFrame({
    'id': np.random.choice(100, 1000000),
    'group': np.random.choice(20, 1000000),
    'term': np.random.choice(10, 1000000)
})

# Convert the pandas DataFrame to a dask DataFrame
dask_df = from_pandas(df)

# Group by id and group, count the number of unique values in each group
result = dask_df.groupby(['id', 'group', 'term']).size().unstack(fill_value=0).compute()

print(result)

By using dask, we can take advantage of multiple CPU cores to speed up our computations.

Conclusion


In this article, we covered how to group a pandas DataFrame by multiple columns and count the number of unique values in each group. We explored different approaches, including using groupby with size, as well as some performance optimization techniques such as chunking and using dask. By understanding these concepts and applying them to our code, we can improve the performance and efficiency of our data analysis tasks.

Further Reading



Last modified on 2025-03-09