2017-08-11 106 views
3

我有一個系列,看起來像:如何對索引和值的熊貓系列進行排序?

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 

我想用價值來訂購吧,還通過索引。所以我會有最小的數字,但要尊重索引的字母順序。

回答

2

使用np.lexsort得到有序的位置,並傳遞給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 
+0

你知道爲什麼我的失敗? – ayhan

+0

嗯,它已經與mergesort修復。無論如何,很高興知道這個選擇。 :) – ayhan

+0

@ayhan ahh是的,穩定與不穩定的排序。 – piRSquared

2

我想你可以先將這個系列轉移到DataFrame中,其中的列是索引和系列值。然後對DataFrame進行排序。要做轉移,只需撥打本系列的reset_index功能並設置drop=False,即可獲得數據幀。

這裏有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