繁体   English   中英

Python数据框:随机排列行

[英]Python Dataframe: Shuffle group of rows

混洗数据帧中的一组行的最佳方法是什么? 需要这个用于改组模型的火车。

例如,每隔10行作为一个单独的组进行洗牌,或者具有某种逻辑条件以创建单独的组并将它们作为一个组洗牌。

如果使用要分组的索引创建新列,则可以执行以下操作:

groups = [df.sample(frac=1) for _, df in df.groupby('index_to_group_on')]
return pandas.concat(groups)

例如,如果您想随机播放每组10行,可以通过以下方式创建此索引:

df['group_of_ten'] = numpy.arange(len(df)/10)

如果您要进行交叉验证,则可以查看scikit-learn的train_test_splithttp : train_test_split

也可能有其他方法,一种方法可能是使用sklearn shuffle 您可以对要混排的n行进行切片,并使用.append append剩余的其他行到.append的结果中。

from sklearn.utils import shuffle

# if df is the dataframe to then:
n = 10 # number of rows to shuffle
shuffled_df = shuffle(df[:n]).append(df[n:])

您可以做的是-创建一个标识组的列,然后按该列分组,然后随机分组每个组。

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df['group_id'] = np.arange(df.shape[0]) // 10  # // is integer division in python3, won't work in python2
shuffled_groups = [v.drop(['group_id'], axis=1).sample(frac=1).reset_index(drop=True) for k, v in df.groupby('group_id')]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM