pceccon
pceccon

Reputation: 9844

How to sort a pandas series of both index and values?

I have a Series that looks like:

146tf150p    1.000000
havent       1.000000
home         1.000000
okie         1.000000
thanx        1.000000
er           1.000000
anything     1.000000
lei          1.000000
nite         1.000000
yup          1.000000
thank        1.000000
ok           1.000000
where        1.000000
beerage      1.000000
anytime      1.000000
too          1.000000
done         1.000000
645          1.000000
tick         0.980166
blank        0.932702
dtype: float64

I would like to order it by value, but also by index. So I would have smallest numbers at top but respecting the alphabetical order of the indexes.

Upvotes: 9

Views: 5022

Answers (2)

piRSquared
piRSquared

Reputation: 294488

Use np.lexsort to get the ordered positions and pass to iloc

ser.iloc[np.lexsort([ser.index, ser.values])]

blank        0.932702
tick         0.980166
146tf150p    1.000000
645          1.000000
anything     1.000000
anytime      1.000000
beerage      1.000000
done         1.000000
er           1.000000
havent       1.000000
home         1.000000
lei          1.000000
nite         1.000000
ok           1.000000
okie         1.000000
thank        1.000000
thanx        1.000000
too          1.000000
where        1.000000
yup          1.000000
dtype: float64

Upvotes: 8

rojeeer
rojeeer

Reputation: 2011

I think you could first transfer this series to a DataFrame, of which the columns are the index and value of series. Then sort the DataFrame. To do the transfer, just call reset_index function of this series and set drop=False, then you can get a dataframe.

Here are exmples:

In [38]: s
Out[38]: 
0
146tf150p    1.000000
havent       1.000000
home         1.000000
okie         1.000000
thanx        1.000000
er           1.000000
anything     1.000000
lei          1.000000
nite         1.000000
yup          1.000000
thank        1.000000
ok           1.000000
where        1.000000
beerage      1.000000
anytime      1.000000
too          1.000000
done         1.000000
645          1.000000
tick         0.980166
blank        0.932702
Name: 1, dtype: float64
# drop=False, convert to a dataframe
In [39]: df = s.reset_index(drop=False)
# sorting by two columns, first 1 then 0
In [40]: df.sort_values([1,0])
Out[40]: 
            0         1
19      blank  0.932702
18       tick  0.980166
0   146tf150p  1.000000
17        645  1.000000
6    anything  1.000000
14    anytime  1.000000
13    beerage  1.000000
16       done  1.000000
5          er  1.000000
1      havent  1.000000
2        home  1.000000
7         lei  1.000000
8        nite  1.000000
11         ok  1.000000
3        okie  1.000000
10      thank  1.000000
4       thanx  1.000000
15        too  1.000000
12      where  1.000000
9         yup  1.000000
# sorting by two columns, first 0 column then 1 column
In [41]: df.sort_values([0, 1])
Out[41]: 
            0         1
0   146tf150p  1.000000
17        645  1.000000
6    anything  1.000000
14    anytime  1.000000
13    beerage  1.000000
19      blank  0.932702
16       done  1.000000
5          er  1.000000
1      havent  1.000000
2        home  1.000000
7         lei  1.000000
8        nite  1.000000
11         ok  1.000000
3        okie  1.000000
10      thank  1.000000
4       thanx  1.000000
18       tick  0.980166
15        too  1.000000
12      where  1.000000
9         yup  1.000000

Upvotes: 8

Related Questions