Pandas GroupBy - Count the occurrences of each combination

Pandas GroupBy - Count the occurrences of each combination

To count the occurrences of each combination in a pandas DataFrame using groupby(), you can group by multiple columns and then use the size() method. This method returns the count of each combination. The result is a Series with multi-level indexing.

Here's a step-by-step example:

Step 1: Create a sample DataFrame:

import pandas as pd data = { 'A': ['foo', 'foo', 'foo', 'bar', 'bar', 'foo'], 'B': ['one', 'one', 'two', 'two', 'one', 'two'] } df = pd.DataFrame(data) print(df) 

This will produce:

 A B 0 foo one 1 foo one 2 foo two 3 bar two 4 bar one 5 foo two 

Step 2: Group by columns 'A' and 'B' and count occurrences:

grouped = df.groupby(['A', 'B']).size().reset_index(name='Count') print(grouped) 

The output will be:

 A B Count 0 bar one 1 1 bar two 1 2 foo one 2 3 foo two 2 

The reset_index() function is used to convert the multi-index series into a DataFrame. The name='Count' argument assigns a name to the count column.

This resulting DataFrame (grouped) shows the count of each combination in the original DataFrame.


More Tags

presentation outputstream android-5.0-lollipop sql-server-2014-express zpl-ii r-rownames google-cloud-dataflow uistackview flask-migrate netsh

More Programming Guides

Other Guides

More Programming Examples