1
如何在熊貓羣后對羣進行採樣?假設我想在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')
有「約翰」,「喬治」,「安德魯」,「大牛」 4組分組,我感興趣的是得到2組出了4不要緊哪它返回2組。
非常感謝。
如果這是您想要的輸出,則不需要「groupby」。簡單地做:'selected_names = np.random.choice(df.name.unique(),2,replace = False)''後面跟'df [df.name.isin(selected_names)]' – cwharland 2014-12-05 15:39:21
Good catch @cwharland – BlueFeet 2014-12-05 15:58:05