【发布时间】:2021-09-29 19:36:36
【问题描述】:
以下是生成示例数据框的一些代码:
fruits=pd.DataFrame()
fruits['month']=['jan','feb','feb','march','jan','april','april','june','march','march','june','april']
fruits['fruit']=['apple','orange','pear','orange','apple','pear','cherry','pear','orange','cherry','apple','cherry']
ind=fruits.index
ind_mnth=fruits['month'].values
fruits['price']=[30,20,40,25,30 ,45,60,45,25,55,37,60]
fruits_grp = fruits.set_index([ind_mnth, ind],drop=False)
如何在这个多索引数据帧中随机打乱外部索引和内部索引以不同的随机顺序?
【问题讨论】:
-
你真的要去掉内索引和外索引之间的关联吗?
-
不,我希望执行 2 级随机播放。首先打乱外部索引(月),然后在相同的外部索引(月)中打乱内部索引。
-
请参考正在执行的类似任务,stackoverflow.com/questions/55054185/…,不同之处在于我想以随机顺序对外部索引和内部索引进行洗牌。
-
您可以对数据框进行采样
df.sample(frac=1) -
这能回答你的问题吗? Shuffle DataFrame rows
标签: pandas dataframe multi-index shuffle