np.argsort 如何在 Pandas DataFrame 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39174810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How np.argsort works in pandas DataFrame?
提问by Karthik Elangovan
I have the following pandas DataFrame named index
:
我有以下 Pandas DataFrame 命名index
:
tz
521.0
Africa/Cairo 3.0
Africa/Casablanca 1.0
Africa/Ceuta 2.0
Africa/Johannesburg 1.0
dtype: float64
when I apply index.argsort()
I get something like this:
当我申请时,index.argsort()
我得到这样的信息:
tz
2
Africa/Cairo 4
Africa/Casablanca 3
Africa/Ceuta 1
Africa/Johannesburg 0
dtype: int64
Can someone explain to me where the numbers: 2,4,3,1,0
come? I know they're index range from 0 to 4 but I can't find any logic in their order.
有人可以向我解释数字在哪里:2,4,3,1,0
来吗?我知道它们的索引范围从 0 到 4,但我找不到它们的顺序中的任何逻辑。
回答by piRSquared
argsort
returns the index positions of the values being sorted if they were to be sorted. Keep in mind that this is a numpy function and its assignment to series or dataframe indices is erroneous.
argsort
如果要排序,则返回正在排序的值的索引位置。请记住,这是一个 numpy 函数,它对系列或数据帧索引的分配是错误的。
2
refers to the item in the2
position (3rd) was the minimum- this was
1.0
- this was
4
refers to the item in the4
position (5th) was next- also
1.0
- also
3
(4th position) was a2.0
1
(2nd position) was a3.0
0
(1st position) was a521.0
and the maximum
2
指2
位置 (3rd) 中的项目是最小的- 这是
1.0
- 这是
4
指4
位置(第 5 个)中的项目是下一个- 还
1.0
- 还
3
(第 4 位)是一个2.0
1
(第二个位置)是一个3.0
0
(第一个位置)是一个521.0
和最大值
It's more appropriate to assign to an array and use as a slice
分配给数组并用作切片更合适
a = s.values.argsort()
s.iloc[a]
tz
Africa/Casablanca 1.0
Africa/Johannesburg 1.0
Africa/Ceuta 2.0
Africa/Cairo 3.0
521.0
Name: value, dtype: float64