【发布时间】:2019-06-29 03:53:23
【问题描述】:
我有一个熊猫系列:
import numpy as np
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.Series(np.random.randn(8), index=index)
s
Out[3]:
first second
bar one -1.111475
two -0.644368
baz one 0.027621
two 0.130411
foo one -0.942718
two -1.335731
qux one 1.277417
two -0.242090
dtype: float64
如何按每个组中的值对这个系列进行排序?
例如,qux 组的第一行应该有两个,-0.242090,然后是第一行,1.277417。 组栏排序良好,因为 -1.111475 低于 -0.644368。
我需要像 s.groupby(level=0).sort_values() 这样的东西。
【问题讨论】:
标签: python pandas sorting series multi-index