I have a relatively tricky iteration question that I am having trouble implementing.
I have a dataframe with the first 6 columns seen below. I am trying to write a function that iterates within groups -- specifically grouping rows by Category and Level -- and then generates a new variable if two conditions are met for that row vs. any other row in the group. I'd like to generate the Opportunity? binary indicator below, where it equals 1 if it matches the condition. The Reason column just provides an explanation of the result I want to generate.
Logic:For each id_group, if ((metric_LHS[entity]>Metric_RHS[other entity in group]) & (metric_LHS[entity]>Baseline[entity])), Opportunity? = 1
So in my example, the Opportunity? column equals 1 for Jim because Metric_LHS(Jim) > Metric_RHS(Jack) and Metric_LHS(Jim)>Baseline(Jim). Meanwhile, Rick is a 0, for example, because the criteria does not work for the only other person in the group, Joe.
See below for some pieces of the code and logic that I have written. My question is the following: How do I iterate within each row of each group and compare that row with every other row in that group?
id_group=df.groupby(['Category','Level'])
for row in id_group:
df['Opportunity?'](([df[metric_LHS][row]>df[Metric_RHS][row+1]) &\
(df[metric_LHS][row]>df[Baseline][row])) = 1
***How to iterate to next row in group?***
