How do I sequentially get the index and column of the highest, next highest, etc. number in a pandas dataframe?

Aephir

I have a pandas DataFrame of the general form (let's call it df)

    A       C       D       E       F
0   0.4     0.1     0.2     0       0.7
1   0       0.2     0       0.5     0.6
2   0.1     0.5     0       0       0
3   0       0       0       0.8     0
4   0.8     0.1     0       0       0
5   0       0.9     0       0       0

Though each axis will be longer (21 columns, varying index length).

I need to find the largest number in the entire DataFrame, and extract the index and column name, then the next largest (if not in same index) and again find index and column name, and so on.

I can of course extract each index as a series (pd.Index.to_series) or list, and do it item-wise. But I'm thinking there's a more efficient way using the dataframes (since I cannot predict the maximum index size this will eventually be used on), that I just can't seem to find/think of.

I've found plenty of answer using df.max() or df.idmax(), to get the maximum, if I only needed that.

Tom McLean

Use the following:

df.stack().reset_index().sort_values(by=0, ascending=False)
Out[168]: 
    level_0 level_1    0
26        5       C  0.9
18        3       E  0.8
20        4       A  0.8
4         0       F  0.7
9         1       F  0.6
8         1       E  0.5
11        2       C  0.5
0         0       A  0.4
6         1       C  0.2
2         0       D  0.2
10        2       A  0.1
1         0       C  0.1
21        4       C  0.1
27        5       D  0.0
28        5       E  0.0
25        5       A  0.0
24        4       F  0.0
23        4       E  0.0
22        4       D  0.0
15        3       A  0.0
19        3       F  0.0
17        3       D  0.0
16        3       C  0.0
14        2       F  0.0
13        2       E  0.0
12        2       D  0.0
7         1       D  0.0
5         1       A  0.0
3         0       E  0.0
29        5       F  0.0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get Column and Row Index for Highest Value in Dataframe Pandas

How do I select related subtopic videos with the highest episode number?

How do i get the highest ID of an item within a database in djngo?

How to get second highest value in a pandas column for a certain ID?

Highest and lowest column values from multi index pandas dataframe

Get the highest number or the next available number in a list

How do I count the number of items in an array that start with the highest number?

How to highlight where a number in another cell falls in a column? Bracket it with the next highest and next lowest?

How do I keep the lines with the highest number where a pattern is matched?

How to get the highest and lowest number

SQL Server: How do I get the highest value not set of an int column?

How do I find the highest count of the same values with Pandas Python?

How do i get the highest ID number of a table and increment it?

Get the number of highest ID number column

Swift: Get next highest number that ends in zero

How do I get the highest property from a laravel eloquent object

How do I find the array index with its highest value with vhdl?

How do i get the highest available number in a substring of a list item

DynamoDb: how to get the item based on a column having the highest number?

In MS Excel (Office 2016), how do I select a column NEXT to the highest value?

How do I add a column to a pandas dataframe which has the highest value in a range but applying it to every row?

How do I get the value from 1 column based on highest value of another column of the same row?

How do i select a whole row based on the highest value of a column

How do I select the 3 columns with highest values from a row in a Pandas dataframe?

How do I get the name of the column with the highest value in a row pandas

How to print the three rows with the highest values in a single column in a pandas dataframe

How do I make a colored border to the highest number of population in Javascript?

How do I get the time of the highest quote of the day in KDB?

How do I order by highest number of likes on blog posts?