按行而不是按列对熊猫数据帧进行排名?

时间:2020-04-13 11:36:16

标签: pandas

是否可以按行而不是按列对熊猫数据框进行排名?

排名应该从最高到最小。

示例数据:

Item    Apple    Orange    Pear    Watermelon     Grape
Weight     72        94      55            34        78

我想将排名放在数据框中,这样我的输出是:

Item    Apple    Orange    Pear    Watermelon     Grape
Weight     72        94      55            34        78
Rank        3         1       4             5         2

非常感谢

1 个答案:

答案 0 :(得分:1)

如果只有一行ascending=Falseaxis=1df.loc['rank'] = df.rank(axis=1, ascending=False).astype(int).squeeze() 一起使用DataFrame.rank

df.loc['Rank'] = df.loc['Weight'].rank(ascending=False).astype(int)

或者可以选择行并使用Series.rank

print (df)
        Apple  Orange  Pear  Watermelon  Grape
Item                                          
Weight     72      94    55          34     78
rank        3       1     4           5      2

  // Called every second
  videoWorker.onmessage = function (e) {
    let blob = e.data;
    console.log(blob);
    let blobURL = URL.createObjectURL(blob);
    livePlayer.src = blobURL;
  };