I tried to apply the solution presented in this question to my real data: Selecting rows in a multi-indexed framework . For some reason I canβt get the results that he should give. I attached both a data frame for selection, as well as the result.
What I need,
Lines 3, 11, and 12 should be returned (when you add 4 columns in sequence, 12 should also be selected. This is not now).
df_test = pd.read_csv('df_test.csv') def find_window(df): v = df.values s = np.vstack([np.zeros((1, v.shape[1])), v.cumsum(0)]) threshold = 0 r, c = np.triu_indices(s.shape[0], 1) d = (c - r)[:, None] e = s[c] - s[r] mask = (e / d < threshold).all(1) rng = np.arange(mask.shape[0]) if mask.any(): idx = rng[mask][d[mask].argmax()] i0, i1 = r[idx], c[idx] return pd.DataFrame( v[i0:i1], df.loc[df.name].index[i0:i1], df.columns ) cols = ['2012', '2013', '2014', '2015'] df_test.groupby(level=0)[cols].apply(find_window)
csv_file is here: https://docs.google.com/spreadsheets/d/19oOoBdAs3xRBWq6HReizlqrkWoQR2159nk8GWoR_4-g/edit?usp=sharing
EDIT: correct data added. 

Note: blue frame = rows to be returned, yellow frames are consecutive column values, 0 (threshold).
numpy pandas dataframe
Zanshin
source share