3

For my pandas dataframe, I would like to convert all the values into tuples of the value itself and the column name for instance, a value of "a" in the column "x" would become (a, x). I have not come across a decent way to do this. For dataframe df df[column] does give the name of the column, I have not been able to retrieve it.

Overall, this is related to a larger problem, which I also have not been able to solve, and if there is some straigtforward way to do this, please tell: I have a dataframe of rows of test rounds, and the columns are the participants to the test, and the value is the participants score on the test.

For example:

       John Mary Peter
  1    9      3      3
  2    0      8      5
  3    3      1      4

I would like to organize this into a numpy array where the the names are ordered by their score in the test i.e.

[[John Mary Peter][Mary Peter John][Peter John Mary]]

Any ideas?

1 Answers1

3

Use numpy.argsort in descending order for positions and then broadcasting with columns names converted to numpy array:

arr = df.columns.values[(-df.values).argsort(axis=1)]
print (arr)
[['John' 'Mary' 'Peter']
 ['Mary' 'Peter' 'John']
 ['Peter' 'John' 'Mary']]

Or:

arr = df.columns.values[df.values.argsort(axis=1)[:, ::-1]]
print (arr)
[['John' 'Peter' 'Mary']
 ['Mary' 'Peter' 'John']
 ['Peter' 'John' 'Mary']]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252