from pandas import DataFrame,Series import pandas as pd import numpy as np # 使用numpy.random.permutation可实现对Series或DataFrame的列排列 df = DataFrame(np.arange(5*4).reshape(5,4)) print(df) ''' 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 3 12 13 14 15 4 16 17 18 19 ''' sampler = np.random.permutation(5) # 随机采样 print(sampler) ''' [3 4 1 0 2] ''' print(df.take(sampler)) ''' 0 1 2 3 3 12 13 14 15 4 16 17 18 19 1 4 5 6 7 0 0 1 2 3 2 8 9 10 11 '''