简体   繁体   中英

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','Daniel','george','andrew','Daniel'], 'hits':[12,34,13,23,53,47,20,48]})
In [196]: grouped = df.groupby('name')

There are 'john', 'george', 'andrew', 'daniel' 4 groups in grouped and I'm interested in getting 2 groups out of the 4. It doesn't matter which 2 groups it returns.

Thank you very much.

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')

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM