Ok it was easier than what I thought! Inspired by this example I changed the code to:
scipy.stats.kruskal(*[group["variable"].values for name, group in df.groupby("treatment") if group["variable"].values.size > 4])