При итерации таким способом по объекту groupby возвращаемый объект будет кортежем (index
, group
).
Чтобы перебрать строки для каждого group
, вы можете использовать DataFrame.itterrows
.
Примерно так:
id_group=df.groupby(['Category','Level'])
for g_idx, group in id_group:
for r_idx, row in group.iterrows():
if (((row['Metric_LHS'] > group['Metric_RHS']).any())
& (row['Metric_LHS'] > row['Baseline'])):
df.loc[r_idx, 'Opportunity?'] = 1
Рабочий пример с данными предоставленной вами игрушки
df = pd.DataFrame({'Name':['Jim', 'Jack', 'Greg', 'Alex', 'Steve', 'Jack', 'Rick', 'Joe', 'Bill', 'Dave', 'Dan'],
'Category':['South']*3 + ['North']*3 + ['West']*3 + ['East']*2,
'Level': [1,1,2,2.5,2.5,2.5,3,3,3.25,4,4],
'Metric_LHS': [100,80,70,110,90,105,110,111,90,87,83],
'Metric_RHS': [120,90,75,115,95,110,112,113,95,90,85],
'Baseline': [95,np.nan,73,112,85,103,105,112,93,75,81],
'Opportunity?': [np.nan]*11})
id_group=df.groupby(['Category','Level'])
for g_idx, group in id_group:
for r_idx, row in group.iterrows():
if (((row['Metric_LHS'] > group['Metric_RHS']).any())
& (row['Metric_LHS'] > row['Baseline'])):
df.loc[r_idx, 'Opportunity?'] = 1
print(df)
Name Category Level Metric_LHS Metric_RHS Baseline Opportunity?
0 Jim South 1.00 100 120 95.0 1.0
1 Jack South 1.00 80 90 NaN NaN
2 Greg South 2.00 70 75 73.0 NaN
3 Alex North 2.50 110 115 112.0 NaN
4 Steve North 2.50 90 95 85.0 NaN
5 Jack North 2.50 105 110 103.0 1.0
6 Rick West 3.00 110 112 105.0 NaN
7 Joe West 3.00 111 113 112.0 NaN
8 Bill West 3.25 90 95 93.0 NaN
9 Dave East 4.00 87 90 75.0 1.0
10 Dan East 4.00 83 85 81.0 NaN