Home >Backend Development >Python Tutorial >How to Efficiently Count Term Occurrences within ID-Group Combinations in a Pandas DataFrame?

How to Efficiently Count Term Occurrences within ID-Group Combinations in a Pandas DataFrame?

Linda Hamilton
Linda HamiltonOriginal
2024-12-06 15:23:13586browse

How to Efficiently Count Term Occurrences within ID-Group Combinations in a Pandas DataFrame?

How to performgroupby value counts on a pandas dataframe?

You need to group the dataframe by the 'id' and 'group' columns and calculate the count of each unique value in the 'term' column for each unique pair of 'id' and 'group' values. In other words, you need to determine how often each term appears in each id-group combination.

You can achieve this using pandas' groupby and size functions:

df.groupby(['id', 'group', 'term']).size().unstack(fill_value=0)

This will produce a table with the id and group as indices and the term as columns.

Timing

This method is highly efficient, even for large datasets. For a dataframe with 1,000,000 rows, the above code takes only about 6 seconds to execute.

The above is the detailed content of How to Efficiently Count Term Occurrences within ID-Group Combinations in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn