pandas抽样函数sample

(a)n为样本量
从df中随机抽取n行

df.sample(n=5)

(b)frac为抽样比

df.sample(frac=0.05)

按照这个比例抽取

(c)replace为是否放回

df.sample(n=35,replace=True).index.is_unique

注意这里的.is_unique

(d)axis为抽样维度,默认为0,即抽行

(e)weights为样本权重,自动归一化

df.sample(n=3,weights=np.random.rand(df.shape[0])).head()
#以某一列为权重,这在抽样理论中很常见
#抽到的概率与Math数值成正比
df.sample(n=3,weights=df['Math']).head()

你可能感兴趣的:(笔记)