通过具有整数索引的已排序 pandas 系列中的位置访问值 [英] Access value by location in sorted pandas series with integer index
本文介绍了通过具有整数索引的已排序 pandas 系列中的位置访问值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有整数索引的pandas系列,该索引已按值排序(按值排序),如何在该系列中按位置按值访问值.
I have a pandas Series with an integer index which I've sorted (by value), how I access values by position in this Series.
例如:
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
但是我想获得值0.000122,即该项目位于位置3.
我该怎么办?
But I would like to get the value 0.000122 i.e. the item in position 3.
How can I do this?
推荐答案
替换行
b = np.sort(a)
与
b = pd.Series(np.sort(a), index=a.index)
这将对值进行排序,但保留索引.
This will sort the values, but keep the index.
要获取排序后的系列中的第四个值:
To get the fourth value in the sorted Series:
np.sort(a).values[3]
这篇关于通过具有整数索引的已排序 pandas 系列中的位置访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文