idx = df1.groupby('bar').apply(lambda x: (sum(x['foo'] < threshold) >=
    n_below) & (sum(x['foo'] > threshold) >= n_above))
Filter out groups that do not have a sufficient number of rows meeting a condition