Home > Enterprise >  Determining the order of a set of columns based on their values
Determining the order of a set of columns based on their values

Time:09-22

I am attempting to add another column to my DataFrame that provides a string based on the values of a set of columns. For example the indexes are kids in a classroom. The values are their test scores for different subject. The additional column indicates the order of their best subjects.

Name | Math  | Eng  | Sci  | Law  | Econ |      | Additional column
---------------------------------------------------
David| 45    | 56.0 | 48.2 | 64.2 | 75.4 |      | Econ, Law, Eng, Math, Sci
Sarah| 63    | 78.0 | 63.7 | 48.7 | 89.2 |      | Econ, Eng, Sci, Math, Law
.....| ..    | .... | .... | .... | .... |      | ...
Cindy| 89    | 55.2 | 91.6 | 58.0 | 85.8 |      | Sci, Math, Econ, Law, Eng

I have tried iterating through each row and printing out a separate list of the ordered columns but it comes out as an error before even inserting it to the original DataFrame.

   new_column = [] 
   for i in range(len(test_results)):
      test_results.sort_values(by=test_results.loc[i], axis=1, ascending=False)
      new_column.append(list(test_results.columns.values))
      print(new_column)

Happy to get some new suggestions on how to solve this.

CodePudding user response:

You can try this:

df['Additional column'] = df.apply(lambda x: list(x.sort_values(ascending=False).index), axis=1)
  • Related