I have the following code to filter out groups with size less than 4 and greater than 51. I am issuing two statements for the same, which I believe is very inefficient. Is there a way I can say 51 > 'size' > 4
df = df[df.groupby('id',sort=False)['id'].transform('size') > 4]
df = df[df.groupby('id')['id'].transform('size') < 51]