简体   繁体   中英

Pythonic way to put specified value at last position when sorting

For series pd.Series(['d', 'b', 'a', 'b', 'c', 'a', 'd']) , how to sort it so that:

  • a is the last item of the series
  • all other items are ascending

So desired outcome will be: pd.Series(['b', 'b', 'c', 'd', 'd', 'a', 'a'])

Thanks

Sort in two steps:

s = pd.Series(['d', 'b', 'a', 'b', 'c', 'a', 'd'])

s.sort_values().pipe(lambda x: x.iloc[x.eq('a').values.argsort(kind='mergesort')])
# use mergesort to make sure the sorting is stable so the second sort doesn't change 
# the first sorting order when values are a and not a separately

#1    b
#3    b
#4    c
#0    d
#6    d
#2    a
#5    a
#dtype: object

Or create a dummpy data frame with an extra column indicating whether the values are equal to a , then sort by the two columns:

pd.concat([s.rename('s'), s.eq('a').rename('a')], axis=1).sort_values(['a', 's']).s

#1    b
#3    b
#4    c
#0    d
#6    d
#2    a
#5    a
#Name: s, dtype: object

您可以通过对不是'a'的值进行排序,然后附加所有'a'值来形成排序后的序列。

sorted_series = ser[ser != 'a'].append(ser[ser == 'a'])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM