【发布时间】:2017-06-16 09:50:49
【问题描述】:
鉴于以下情况:
import pandas as pd
arrays = [['bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'baz'],
['total', 'two', 'one', 'two', 'four', 'total', 'five']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.Series(np.random.randn(7), index=index)
s
first second
bar total 0.334158
two -0.267854
one 1.161727
baz two -0.748685
four -0.888634
total 0.383310
five 0.506120
dtype: float64
如何确保“总”行(每个第二个索引)总是像这样位于每个组的底部?:
first second
bar one 0.210911
two 0.628357
total -0.911331
baz two 0.315396
four -0.195451
five 0.060159
total 0.638313
dtype: float64
【问题讨论】:
-
最简单的选择是调用它
"~total"或"|total|"或"{total}"。然后它总是会被排序到底部。 -
请不要在问题中提及截止日期:请记住,几乎所有回答的人都是志愿者。
-
很抱歉。
标签: python sorting pandas multi-index