Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating pandas dataframe values per column name

Tags:

python

pandas

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?

like image 563
murtourpo95 Avatar asked Jan 30 '26 23:01

murtourpo95


1 Answers

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']]
like image 89
jezrael Avatar answered Feb 01 '26 13:02

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!