How to get maximum values in a row and call the proper name of the appropriate column with pandas

Question:

I want to get the maximum values in a row and print the value and the name of the appropriate column.

s1 = pd.Series([5, 6, 7, 10, 12, 6, 8, 55, 9])
s2 = pd.Series([7, 8, 9, 16, 13, 8, 2, 11, 7])

df = pd.DataFrame([list(s1), list(s2)],  columns =  ["A", "B", "C", "D", "E", "F", "G", "H", "I"])

   A  B  C   D   E  F  G   H  I
0  5  6  7  10  12  6  8  55  9
1  7  8  9  16  13  8  2  11  7

I want to choose for example "index 0" and get something like this:

55 H
12 E
10 D
9 I
Asked By: Václav Musil

||

Answers:

You can sort and then take the top N values:

>>> df.loc[0].sort_values(ascending=False).iloc[:4]
H    55
E    12
D    10
I     9
Name: 0, dtype: int64

As a function:

def top_n(idx, n):
    return df.loc[idx].sort_values(ascending=False).iloc[:n]
Answered By: msailor

Sorting is relatively expensive (O(n*log(n)) complexity).

Use nlargest:

out = df.loc[0].nlargest(4)

Output:

H    55
E    12
D    10
I     9
Name: 0, dtype: int64
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.