Pandas - Groupby value counts on the DataFrame

Pandas - Groupby value counts on the DataFrame

To perform a groupby operation followed by a value_counts on a Pandas DataFrame, you can use the groupby method and then chain the value_counts function. This is particularly useful when you want to count the frequency of values within groups.

Here's a simple example to demonstrate this concept:

Imagine you have a dataset with fruits and their respective colors:

import pandas as pd data = { 'fruit': ['apple', 'banana', 'apple', 'apple', 'banana', 'banana', 'orange', 'orange'], 'color': ['red', 'yellow', 'green', 'red', 'yellow', 'green', 'orange', 'green'] } df = pd.DataFrame(data) 

If you want to count the number of occurrences of each color for each fruit, you can use groupby and value_counts:

result = df.groupby('fruit')['color'].value_counts().unstack(fill_value=0) print(result) 

The output would look like:

 green orange red yellow fruit apple 1 0 2 0 banana 1 0 0 2 orange 1 1 0 0 

This output indicates that there's 1 green apple, 2 red apples, 2 yellow bananas, 1 green banana, 1 orange-colored orange, and 1 green orange in the dataset.

The unstack method is used to pivot the grouped data so that the colors are shown as columns. The fill_value=0 argument ensures that if there are any NaN values due to some colors not being present for a fruit, they are replaced by 0.


More Tags

plotly x509certificate2 jdbctemplate odoo-12 sql-server-group-concat var pull-to-refresh flask launchctl transform

More Programming Guides

Other Guides

More Programming Examples