python排列和随机采样permutation&sample

python排列和随机采样permutation&sample


### Permutation and Random Sampling
# 排列和随机采样
# 利⽤numpy.random.permutation函数可以轻松实现对Series或
# DataFrame的列的排列⼯作(permuting,随机重排序)。通过
# 需要排列的轴的⻓度调⽤permutation,可产⽣⼀个表示新顺序
# 的整数数组:
df = pd.DataFrame(np.arange(5 * 4).reshape((5, 4)))
sampler = np.random.permutation(5)
sampler
array([2, 1, 3, 0, 4])
df
df.take(sampler)
df.sample(n=3)
choices = pd.Series([5, 7, -1, 6, 4])
draws = choices.sample(n=10, replace=True)
draws
choices = pd.Series([5, 7, -1, 6, 4])
draws = choices.sample(n=10, replace=True)
draws

你可能感兴趣的:(Python,pandas)