I have a pandas series:
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
How to sort this series by values within each group?
For example, qux group should have the first row with two, -0.242090, and then row one, 1.277417. Group bar is sorted well because -1.111475 is lower than -0.644368.
I need somethin like s.groupby(level=0).sort_values().
Use sort_values
:
np.random.seed(0)
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 = (s.reset_index(name='value')
.sort_values(['first', 'value'])
.set_index(['first', 'second'])['value'])
s.name = None
print(s)
first second
bar two 0.400157
one 1.764052
baz one 0.978738
two 2.240893
foo two -0.977278
one 1.867558
qux two -0.151357
one 0.950088
dtype: float64
You can use np.lexsort
to sort first by your first index level, and second by values.
np.random.seed(0)
s = pd.Series(np.random.randn(8), index=index)
s = s.iloc[np.lexsort((s.values, s.index.get_level_values(0)))]
print(s)
# first second
# bar two 0.400157
# one 1.764052
# baz one 0.978738
# two 2.240893
# foo two -0.977278
# one 1.867558
# qux two -0.151357
# one 0.950088
# dtype: float64
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.