I found the below example for randomly selecting the elements of a single key groupby, however this does not work with a multi-key groupby. From, How to access pandas groupby dataframe by key 创建分组对象 create groupby objectgrouped = df.groupby('some_key')选择N个数据帧并获取其索引pick N dataframes and grab their indicessampled_df_i = random.sample(grouped.indices, N)使用groupby对象"get_group"方法获取组grab the groups using the groupby object 'get_group' methoddf_list = map(lambda df_i: grouped.get_group(df_i),sampled_df_i)(可选)-将其全部转换回单个数据框对象optionally - turn it all back into a single dataframe objectsampled_df = pd.concat(df_list, axis=0, join='outer')推荐答案您可以对df.some_key.unique()的唯一值进行随机抽样,然后使用该样本对df进行切片,最后对所得结果中的groupby进行切片:You can take a randoms sample of the unique values of df.some_key.unique(), use that to slice the df and finally groupby on the resultant:In [337]:df = pd.DataFrame({'some_key': [0,1,2,3,0,1,2,3,0,1,2,3], 'val': [1,2,3,4,1,5,1,5,1,6,7,8]})In [338]:print df[df.some_key.isin(random.sample(df.some_key.unique(),2))].groupby('some_key').mean() valsome_key0 1.0000002 3.666667如果有多个groupby键:If there are more than one groupby keys:In [358]:df = pd.DataFrame({'some_key1':[0,1,2,3,0,1,2,3,0,1,2,3], 'some_key2':[0,0,0,0,1,1,1,1,2,2,2,2], 'val': [1,2,3,4,1,5,1,5,1,6,7,8]})In [359]:gby = df.groupby(['some_key1', 'some_key2'])In [360]:print gby.mean().ix[random.sample(gby.indices.keys(),2)] valsome_key1 some_key21 1 53 2 8但是,如果您只是要获取每个组的值,则甚至不需要groubpy,MultiIndex会做到:But if you are just going to get the values of each group, you don't even need to groubpy, MultiIndex will do:In [372]:idx = random.sample(set(pd.MultiIndex.from_product((df.some_key1, df.some_key2)).tolist()), 2)print df.set_index(['some_key1', 'some_key2']).ix[idx] valsome_key1 some_key22 0 33 1 5 这篇关于Python Pandas从Groupby中选择组的随机样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 06:26