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

Time:11-25

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

CodePudding user response:

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]
  • Related