for group_ids in group_ids_list:
    grouped_df = df.groupby(group_id)
for key, group in grouped_df:
    if len(group) > 25000:
        valid_groups.append(group)
group_sizes[key] = len(group)
group_sizes = pd.Series(group_sizes)
Pandas groupby with minimal group size