我有一个multindex DataFrame,df:
arrays = [['bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo', 'foo'],
['one', 'two', 'one', 'two', 'three', 'four', 'one', 'two']]
df = pd.DataFrame(np.ones([8, 4]), index=arrays)
看起来像:
0 1 2 3
bar one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
baz one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
three 1.0 1.0 1.0 1.0
four 1.0 1.0 1.0 1.0
foo one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
现在,我需要将“ baz”子级别分类为新的顺序,以创建类似于df_end的内容:
arrays_end = [['bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo', 'foo'],
['one', 'two', 'two', 'four', 'three', 'one', 'one', 'two']]
df_end = pd.DataFrame(np.ones([8, 4]), index=arrays_end)
看起来像:
0 1 2 3
bar one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
baz two 1.0 1.0 1.0 1.0
four 1.0 1.0 1.0 1.0
three 1.0 1.0 1.0 1.0
one 1.0 1.0 1.0 1.0
foo one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
我以为我可以重新索引baz行:
new_index = ['two','four','three','one']
df.loc['baz'].reindex(new_index)
这使:
0 1 2 3
two 1.0 1.0 1.0 1.0
four 1.0 1.0 1.0 1.0
three 1.0 1.0 1.0 1.0
one 1.0 1.0 1.0 1.0
…并将这些值重新插入到原始DataFrame中:
df.loc['baz'] = df.loc['baz'].reindex(new_index)
但是结果是:
0 1 2 3
bar one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
baz one NaN NaN NaN NaN
two NaN NaN NaN NaN
three NaN NaN NaN NaN
four NaN NaN NaN NaN
foo one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
这不是我要找的东西!所以我的问题是如何使用new_index对baz索引中的行进行重新排序.任何建议将不胜感激.
解决方法:
编辑:(以适合所需的布局)
arrays = [['bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo', 'foo'],
['one', 'two', 'one', 'two', 'three', 'four', 'one', 'two']]
df = pd.DataFrame(np.arange(32).reshape([8, 4]), index=arrays)
new_baz_index = [('baz', i) for i in ['two','four','three','one']]
index = df.index.values.copy()
index[df.index.get_loc('baz')] = new_baz_index
df.reindex(index)
df.index.get_loc(‘baz’)将得到baz零件的位置作为切片对象,我们仅在其中替换零件.