pandas :groupby 后的样本组 [英] pandas: sample groups after groupby
本文介绍了 pandas :groupby 后的样本组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Pandas 的 groupby 之后对组进行采样?假设我想在 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')
分组中有 'john'、'george'、'andrew'、'daniel' 4 个组,我有兴趣从 4 个组中取出 2 个组.没关系它返回哪 2 个组.
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.
非常感谢.
推荐答案
您可以提前对名称进行采样,并仅对所选名称进行分组:
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')
这篇关于 pandas :groupby 后的样本组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文