我有一个在行上DataFrame
使用 a的熊猫:MultiIndex
index = pandas.MultiIndex.from_tuples(list(itertools.product(range(3), range(3))))
df = pandas.DataFrame(numpy.random.randn(9,3), index=index, columns=['A', 'B', 'C'])
A B C
0 0 2.400417 0.698638 1.231540
1 -0.023154 -2.110450 0.774964
2 -1.282392 -0.062794 1.471655
1 0 -1.081853 0.261876 -1.771075
1 -2.013747 -0.377957 -0.393802
2 1.711172 -0.552468 1.018727
2 0 0.155821 -0.222691 0.496586
1 0.563638 -0.756709 1.050212
2 -1.446159 -0.891549 0.256695
我想在索引的第一级对这个 DataFrame 进行洗牌,所以可能的结果是:
A B C
1 0 -1.081853 0.261876 -1.771075
1 -2.013747 -0.377957 -0.393802
2 1.711172 -0.552468 1.018727
0 0 2.400417 0.698638 1.231540
1 -0.023154 -2.110450 0.774964
2 -1.282392 -0.062794 1.471655
2 0 0.155821 -0.222691 0.496586
1 0.563638 -0.756709 1.050212
2 -1.446159 -0.891549 0.256695