熊猫在一定程度上洗牌

时间:2012-05-04 08:43:56

标签: python pandas

我有一个Panda DataFrame在行上使用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

2 个答案:

答案 0 :(得分:4)

reindex方法可以在传递与所需顺序匹配的重新排序的元组数组时完成此操作。此时,可以根据您的问题进行重新排序。例如:

In [38]: df
Out[38]: 
            A         B         C
0 0 -1.725337  0.111493  0.178294
  1 -1.809003 -0.614219 -0.931909
  2  0.621427 -0.186233  0.254727
1 0 -1.322863  1.242415  1.375579
  1  0.249738 -1.280204  0.356491
  2 -0.743671  0.325841 -0.167772
2 0 -0.070937  0.401172 -1.790801
  1  1.433794  2.257198  1.848435
  2 -1.021557 -1.054363 -1.485536

In [39]: neworder = [1, 0, 2]

In [41]: newindex = sorted(df.index, key=lambda x: neworder.index(x[0]))

In [42]: newindex
Out[42]: 
[(1L, 0L),
 (1L, 1L),
 (1L, 2L),
 (0L, 0L),
 (0L, 1L),
 (0L, 2L),
 (2L, 0L),
 (2L, 1L),
 (2L, 2L)]

In [43]: df.reindex(newindex)
Out[43]: 
            A         B         C
1 0 -1.322863  1.242415  1.375579
  1  0.249738 -1.280204  0.356491
  2 -0.743671  0.325841 -0.167772
0 0 -1.725337  0.111493  0.178294
  1 -1.809003 -0.614219 -0.931909
  2  0.621427 -0.186233  0.254727
2 0 -0.070937  0.401172 -1.790801
  1  1.433794  2.257198  1.848435
  2 -1.021557 -1.054363 -1.485536

答案 1 :(得分:0)

如果以下方法有效,but no

会更容易
df.ix[[1, 0, 2]]

以下是更多解决方法。也许有更好的方法,但我无法弄明白。这只会按正确顺序创建DataFrame'切片'列表,并将其与pandas.concat连接起来。

In : df
Out:
            A         B         C
0 0  1.202098 -0.031121  1.417629
  1 -0.895862  0.697531 -0.572411
  2  1.179101 -0.008602  1.583385
1 0  1.969477 -0.968004 -0.567695
  1 -1.504443 -0.002264 -0.413091
  2 -1.412457  0.310518  0.267475
2 0 -0.385933 -0.471800 -0.598141
  1 -0.105032  0.443437 -0.615566
  2 -1.035326 -0.282289 -0.042762

In : shuffled = [2,0,1]

In : df2 = pandas.concat([df.ix[i:i] for i in shuffled])

In : df2
Out:
            A         B         C
2 0 -0.385933 -0.471800 -0.598141
  1 -0.105032  0.443437 -0.615566
  2 -1.035326 -0.282289 -0.042762
0 0  1.202098 -0.031121  1.417629
  1 -0.895862  0.697531 -0.572411
  2  1.179101 -0.008602  1.583385
1 0  1.969477 -0.968004 -0.567695
  1 -1.504443 -0.002264 -0.413091
  2 -1.412457  0.310518  0.267475