Skip to content Skip to sidebar Skip to footer

Pandas: Sample Groups After Groupby

How can I sample groups after a groupby in pandas? Say I want to get the first half of groups after groupby. In [194]: df = pd.DataFrame({'name':['john', 'george', 'john','andrew'

Solution 1:

You can sample the names ahead of time and only group the chosen names:

selected_names = np.random.choice(df.name.unique(),2,replace = False)
grouped = df[df.name.isin(selected_names)].groupby('name')

Solution 2:

Thanks for the quick replies, ajcr and cwharland. I was probably not clear about what I want, but your suggestions are great. I did:

choices =np.random.choice(grouped.indices.keys(), 2, replace=False)
df[df['name'].isin(choices)]

and get the results I hoped:

Out[215]: 
   hits    name
0    12    john
2    13    john
3    23  andrew
6    20  andrew

Thank you both!


Post a Comment for "Pandas: Sample Groups After Groupby"