Location access value in sorted panda series with integer index

I have a pandas series with an integer index that I sorted (by value), as I get the values by position in this series.

For example:

s_original = pd.Series({0: -0.000213, 1: 0.00031399999999999999, 2: -0.00024899999999999998, 3: -2.6999999999999999e-05, 4: 0.000122}) s_sorted = np.sort(s_original) In [3]: s_original Out[3]: 0 -0.000213 1 0.000314 2 -0.000249 3 -0.000027 4 0.000122 In [4]: s_sorted Out[4]: 2 -0.000249 0 -0.000213 3 -0.000027 4 0.000122 1 0.000314 In [5]: s_sorted[3] Out[5]: -2.6999999999999999e-05 

But I would like to get the value 0.000122, i.e. item in position . 3. How can I do this?

+3
python pandas series
Jan 22 '13 at 9:55 on
source share
2 answers

Replace string

 b = np.sort(a) 

from

 b = pd.Series(np.sort(a), index=a.index) 

This will sort the values ​​but keep the index.

EDIT:

To get the fourth value in a sorted series:

 np.sort(a).values[3] 
+6
Jan 22 '13 at 10:00
source share

You can use iget to extract by position:
(In fact, this method was created specifically to overcome this ambiguity.)

 In [1]: s = pd.Series([0, 2, 1]) In [2]: s.sort() In [3]: s Out[3]: 0 0 2 1 1 2 In [4]: s.iget(1) Out[4]: 1 

.

.ix integer index behavior is noted in pandas "gotchas" :

At pandas, our common view is that tags are larger than whole locations . Thus, with an integer axis index, only indexing based on labels with standard tools such as .ix .

This intentional decision was made to prevent ambiguity and subtle errors (many users reported errors when API changes were made to stop the β€œdrop” in position-based indexing).

Note: this will work if you use a non-integer index where .ix not ambiguous.

For example:

 In [11]: s1 = pd.Series([0, 2, 1], list('abc')) In [12]: s1 Out[12]: a 0 b 2 c 1 In [13]: s1.sort() In [14]: s1 Out[14]: a 0 c 1 b 2 In [15]: s1.ix[1] Out[15]: 1 
+4
Jan 22 '13 at 19:38
source share



All Articles