简体   繁体   English

跨多列排序 pandas

[英]Sorting across multiple columns pandas

df = pd.DataFrame([["Alpha", 3, 2, 4], ["Bravo", 2, 3, 1], ["Charlie", 4, 1, 3], ["Delta", 1, 4, 2]], 
              columns = ["Company", "Running", "Combat", "Range"])
print(df)
  Company Running   Combat  Range
0   Alpha      3      2       4
1   Bravo      2      3       1
2   Charlie    4      1       3
3   Delta      1      4       2

Hi, I am trying to sort the the following dataframe so the rows would be arranged such that the best performing across the three columns would be at the top.嗨,我正在尝试对以下 dataframe 进行排序,以便对行进行排列,以使三列中表现最好的位于顶部。 In this case would be Bravo company as it is 2 in running, 3 in drills and 1 in range.在这种情况下,将是 Bravo 公司,因为它是 2 跑步、3 钻和 1 射程。

Would this approach work if the list have a lot more companies and it is hard to know the exact "best performing company"?如果列表中有更多的公司并且很难知道确切的“表现最佳的公司”,这种方法会起作用吗?

I have tried:我努力了:

df_sort = df.sort_values(['Running', 'Combat', 'Range'], ascending=[True, True, True])

current output:当前 output:

    Company Running Combat  Range
1   Delta      1      4     2
0   Bravo      2      3     1
3   Alpha      3      2     4
2   Charlie    4      1     3

but it doesn't turn out how I wanted it to be.但它并没有变成我想要的样子。 Can this be done through pandas?这可以通过 pandas 完成吗? I was expecting the output to be:我期待 output 是:

Company Running Combat  Range
0   Bravo   2     3     1
1   Delta   1     4     2
2   Charlie 4     1     3
3   Alpha   3     2     4

If want sorting by mean s per rows first create mean , then add Series.argsort for positions of sorted values and last change order of values by DataFrame.iloc :如果要按每行的mean排序首先创建mean ,然后为排序值的位置添加Series.argsort ,并按DataFrame.iloc添加值的最后更改顺序:

df1 = df.iloc[df.mean(axis=1).argsort()]
print (df1)
   Company  Running  Combat  Range
1    Bravo        2       3      1
3    Delta        1       4      2
2  Charlie        4       1      3
0    Alpha        3       2      4

EDIT: If need remove some columns before by DataFrame.drop :编辑:如果需要在DataFrame.drop之前删除一些列:

cols = ['Overall','Subordination']
df2 = text_df.iloc[text_df.drop(cols, axis=1).mean(axis=1).argsort()]
print (df2)
   Company  Running  Combat  Overall Subordination  Range
1    Bravo        2       3     0.70          Poor      1
3    Delta        1       4     0.83          Good      2
2  Charlie        4       1     0.81          Good      3
0    Alpha        3       2     0.91     Excellent      4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM