randomly split DataFrame by group?
Oh, there is an easy way !

create a list / array of unique group_ids 
create a random mask for this list 
and use the mask to split the file
I have a DataFrame where multiple rows share group_id values (very large number of groups). 
Is there an elegant way to randomly split this data into training and test data in a way that the training and test sets do not share group_id?
The best process I can come up with right now is
 - create mask from msk = np.random.rand()
 - apply it to the DataFrame
 - check test file for rows that share group_id with training set and move these rows to training set.   
This is clearly non-elegant and has multiple issues (including the possibility that the test data ends up empty). I feel like there must be a better way, is there? 
Thanks