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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:54:07  来源:igfitidea点击:

How np.argsort works in pandas DataFrame?

pythonpython-3.xpandasnumpy

提问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,0come? 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

argsortreturns 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 函数,它对系列或数据帧索引的分配是错误的。

  • 2refers to the item in the 2position (3rd) was the minimum
    • this was 1.0
  • 4refers to the item in the 4position (5th) was next
    • also 1.0
  • 3(4th position) was a 2.0
  • 1(2nd position) was a 3.0
  • 0(1st position) was a 521.0and the maximum
  • 22位置 (3rd) 中的项目是最小的
    • 这是 1.0
  • 44位置(第 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