Skip to content
Advertisement

Pandas: using groupby to calculate a ratio by specific values

enter image description here

Hi I have a dataframe that looks like this:

and I want to calculate a ratio in the column ‘count_number’, based on the values in the column ‘tone’ by this formula: [‘blue’+’grey’]/’red’ per each unite combination of ‘participant_id’, ‘session’, ‘block’ –

here is part of my dataset as text, the left column ‘RATIO’ is my expected output:

participant_id session block tone count_number RATIO 10 1 neg blue 0 0 10 1 neg grey 0 0 10 1 neg red 3 0 10 1 neu blue 1 #DIV/0! 10 1 neu grey 1 #DIV/0! 10 1 neu red 0 #DIV/0! 10 2 neg blue 3 2.333333333 10 2 neg grey 4 2.333333333 10 2 neg red 3 2.333333333 10 2 neu blue 4 1.333333333 10 2 neu grey 0 1.333333333 10 2 neu red 3 1.333333333 11 1 neg blue 0 0 11 1 neg grey 0 0 11 1 neg red 3 0

I tried this (wrong) direction

JavaScript

Advertisement

Answer

Here’s one approach:

We could create separate Series objects for numerator and denominator of the divisions; then groupby + transform sum + div will fetch the desired ratio:

JavaScript

Another approach could be to use groupby + apply a lambda that calculates the required ratio for each group; then map the ratios back to the original DataFrame:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement