n_below = 2
n_above = 2
df1.set_index('bar').loc[df1.groupby('bar').apply(lambda df_sub: (df_sub[
    'foo'] < threshold).sum() >= n_below and (df_sub['foo'] > threshold).
    sum() >= n_above)].reset_index('bar')
Filter out groups that do not have a sufficient number of rows meeting a condition