As a simplified example, suppose I had a DataFrame as follows:
Group Type Value1 Value2
Red A 13 24
Red B 3 12
Blue C 5 0
Red C 8 9
Green A 2 -1
Red None 56 78
Blue A 40 104
Green B 1 -5
What I want to calculate is the difference in Value1 between rows of Type A and B and the difference in Value2 between rows of Type A and B for each Group entry.
Since Red and Green are the only Groups having entries of Type A and B, we would only calculate new rows for these Groups. So the resulting DataFrame would be:
Group Type Value1 Value2
Red A-B 10 12
Green A-B 1 4
My initial idea was simply to filter for rows where Type is either 'A' or 'B' with df = df[df['Type'].isin(['A', 'B'])]
, then filter again for Groups that are in rows with both 'A' and 'B' as Type (not sure how to do this), then sort and apply diff().