与列中的计数成比例地对 Pandas 数据框的行进行采样 [英] Sample rows of pandas dataframe in proportion to counts in a column
问题描述
我有一个大约有 10,000,000 行的大熊猫数据框.每个代表一个特征向量.特征向量以自然组的形式出现,组标签位于名为 group_id
的列中.我想随机抽样 10%
说的行,但与每个 group_id
的数量成比例.
I have a large pandas dataframe with about 10,000,000 rows. Each one represents a feature vector. The feature vectors come in natural groups and the group label is in a column called group_id
. I would like to randomly sample 10%
say of the rows but in proportion to the numbers of each group_id
.
例如,如果 group_id's
是 A, B, A, C, A, Bcode> 那么我希望我的一半采样行具有
group_id
A
,六分之二有 group_id
B
六分之一有 group_id
C代码>.
For example, if the group_id's
are A, B, A, C, A, B
then I would like half of my sampled rows to have group_id
A
, two sixths to have group_id
B
and one sixth to have group_id
C
.
我可以看到熊猫函数sample 但我不确定如何使用它来实现这一目标.
I can see the pandas function sample but I am not sure how to use it to achieve this goal.
推荐答案
可以使用groupby和sample
You can use groupby and sample
sample_df = df.groupby('group_id').apply(lambda x: x.sample(frac=0.1))
这篇关于与列中的计数成比例地对 Pandas 数据框的行进行采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!