方法一: from sklearn.utils import shuffle
方法二:df.sample(frac=1)
方法三:np.random.permutation()+pd.take()
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(50).reshape(10,5),columns=['a','b','c','d','e'])
print(df)
output:
a b c d e
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
4 20 21 22 23 24
5 25 26 27 28 29
6 30 31 32 33 34
7 35 36 37 38 39
8 40 41 42 43 44
9 45 46 47 48 49
eg1:sklearn(机器学习的库)中也有shuffle的方法
from sklearn.utils import shuffle
df = shuffle(df).reset_index(drop=True)
print(df)
output:
eg2:df.sample()
df.sample(frac=1).reset_index(drop=True)
output:
eg3: np.random.permutation()+pd.take()
sample_df = np.random.permutation(len(df))
print(sample_df)
output:
[4 3 6 5 0 2 8 9 1 7]
df_new = df.take(sample_df[:5])
print(df_new)
output:
a b c d e
4 20 21 22 23 24
3 15 16 17 18 19
6 30 31 32 33 34
5 25 26 27 28 29
0 0 1 2 3 4
参考:
https://blog.csdn.net/qq_22238533/article/details/70917102
https://blog.csdn.net/bqw18744018044/article/details/79964599