大 pandas 随机抽取 [英] Pandas random sample with remove
本文介绍了大 pandas 随机抽取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道了DataFrame.sample()
,但是我该怎么做,还要从数据集中删除样本? (注意:AFAIK与替换采样无关)
I'm aware of DataFrame.sample()
, but how can I do this and also remove the sample from the dataset? (Note: AFAIK this has nothing to do with sampling with replacement)
例如,这是我要实现的目标的本质,这实际上是行不通的:
For example here is the essence of what I want to achieve, this does not actually work:
len(df) # 1000
df_subset = df.sample(300)
len(df_subset) # 300
df = df.remove(df_subset)
len(df) # 700
推荐答案
如果您的索引是唯一的
df = df.drop(df_subset.index)
示例
example
df = pd.DataFrame(np.arange(10).reshape(-1, 2))
样本
sample
df_subset = df.sample(2)
df_subset
放置
drop
df.drop(df_subset.index)
这篇关于大 pandas 随机抽取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文